Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 940141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:54:23+00:00 2026-05-15T21:54:23+00:00

I have a UIViewController (called AdjustViewController ) that presents another UIViewController (called SourcePickerViewController )

  • 0

I have a UIViewController (called AdjustViewController) that presents another UIViewController (called SourcePickerViewController) with a UIPickerView modally. I generate instances of the AdjustViewController and they in turn make a SourcePickerViewController. I make an NSDictionary and assign it and an integer to the AdjustViewController and it in turn sets the same properties in the SourcePickerController. This way I can reuse the controllers. The NSDictionary get set up in a UITableViewController that has all the AdjustViewControllers in it.

The problem comes when some of the pickers should have 1 component and some should have 2. The integer that I pass along is called numberOfComponents When I make a picker with numberOfComponents = 1 somehow it’s changing to = 2 but I can’t see how. I have NSLogs all over the place and I can see it happen as soon as the picker delegate method numberOfComponentsInPickerView is called. It’s 1 right before and 2 right after.

There’s obviously more code, but I think I have all the important parts. Although if that were true, maybe I’d know where the problem is!


Inside MenuViewController.m

- (void)viewDidLoad {
    NSLog(@"ChemicalViewController launched");
    self.title = @"Adjust Chemicals";
    NSMutableArray *array = [[NSMutableArray alloc] init];

// Chlorine Controller
    AdjustViewController *chlorineAdjustViewController = [[AdjustViewController alloc] initWithNibName:@"AdjustViewController" bundle:nil];
    chlorineAdjustViewController.title = @"FC - Free Chlorine";
    chlorineAdjustViewController.numberOfComponents = 2;
    NSLog(@"Generating chlorine source dictionary");
    NSDictionary *chlorineSourceDictionary = [self generateChlorineDictionary];
    chlorineAdjustViewController.dictionaryOfSources = chlorineSourceDictionary;
    [chlorineSourceDictionary release];
    [array addObject:chlorineAdjustViewController];
    [chlorineAdjustViewController release];

// CYA Controller
    AdjustViewController *cyaAdjustViewController = [[AdjustViewController alloc] initWithNibName:@"AdjustViewController" bundle:nil];
    cyaAdjustViewController.title = @"CYA - Cyanuric Acid";
    cyaAdjustViewController.numberOfComponents = 1;
    NSLog(@"Generating cya source dictionary");
    NSDictionary *cyaSourceDictionary = [self generateCYADictionary];
    cyaAdjustViewController.dictionaryOfSources = cyaSourceDictionary;
    [cyaSourceDictionary release];
    [array addObject:cyaAdjustViewController];
    [cyaAdjustViewController release];

Inside AdjustViewController.m

// Present the picker for chlorine selection
- (IBAction)getChemicalSource {
    SourcePickerViewController *sourcePickerViewController = [[SourcePickerViewController alloc] init];
    sourcePickerViewController.delegate = self;
    NSLog(@"getChemicalSource setting numberOfComponents %d", self.numberOfComponents);
    sourcePickerViewController.numberOfComponents = self.numberOfComponents;
    NSLog(@"getChemicalSource sending numberOfComponents %d", sourcePickerViewController.numberOfComponents);
    sourcePickerViewController.dictionaryOfSources = self.dictionaryOfSources;
    [self presentModalViewController:sourcePickerViewController animated:YES];
    [sourcePickerViewController release];
}

#pragma mark -
#pragma mark Picker View Delegate Methods

// Returns the values from the picker if a source was chosen
- (void)sourcePickerViewController:(SourcePickerViewController *)controller 
               didSelectSource:(NSString *)source 
              andConcentration:(NSString *)concentration 
                   andConstant:(float)constant 
                   andIsLiquid:(BOOL)isLiquid {


    sourceField.text = [[NSString alloc] initWithFormat:@"%@, %@", source, concentration];
    [self updateAdvice];
    NSLog(@"Returned source = %@, concentration = %@, sourceConstant = %1.7f, isLiquid = %d", source, concentration, constant, isLiquid);
    [self dismissModalViewControllerAnimated:YES];
}

// Returns from the picker without choosing a new source
- (void)sourcePickerViewController:(SourcePickerViewController *)controller 
               didSelectCancel:(BOOL)didCancel {
    [self updateAdvice];
    NSLog(@"Returned without selecting source");
    [self dismissModalViewControllerAnimated:YES];
}

Inside SourceViewController.m

- (void)viewDidLoad {
    NSLog(@"SourcePickerViewController launched");
    NSLog(@"viewDidLoad");
    NSLog(@"Received numberOfComponents %d", self.numberOfComponents);
    self.chemicalSources = dictionaryOfSources;
    NSArray *components = [self.chemicalSources allKeys];
    NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
    self.sources = sorted; // This array has the chemical sources

    if (self.numberOfComponents = 2) {
        NSString *selectedSource = [self.sources objectAtIndex:0];
        NSArray *chemArray = [self.chemicalSources objectForKey:selectedSource];
        NSMutableArray *concentrationArray = [[NSMutableArray alloc] init];
        int num = [chemArray count];
        for (int i=0; i<num; i++) {
            [concentrationArray addObject:[[chemArray objectAtIndex:i] chemConcentration]];
        }
        self.concentrations = concentrationArray;
    }
    [super viewDidLoad];
}

    #pragma mark -
    #pragma mark Picker Data Source Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    NSLog(@"numberOfComponentsInPickerView, self.numberOfComponents = %d", self.numberOfComponents);
    return self.numberOfComponents;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSLog(@"numberOfRowsInComponent, self.numberOfComponents = %d", self.numberOfComponents);
    if (component == kSourceComponent)
        return [self.sources count];
    return [self.concentrations count];
}

#pragma mark Picker Delegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == kSourceComponent)
        return [self.sources objectAtIndex:row];
    return [self.concentrations objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"didSelectRow, self.numberOfComponents = %d", self.numberOfComponents);
    if (numberOfComponents = 2) {
        if (component == kSourceComponent) {
            NSString *selectedSource = [self.sources objectAtIndex:row];
            NSArray *chemArray = [self.chemicalSources objectForKey:selectedSource];
            NSMutableArray *concentrationArray = [[NSMutableArray alloc] init];
            int num = [chemArray count];
            for (int i=0; i<num; i++) {
                [concentrationArray addObject:[[chemArray objectAtIndex:i] chemConcentration]];
            }
    self.concentrations = concentrationArray;
    [picker selectRow:0 inComponent:kConcentrationComponent animated:YES];
    [picker reloadComponent:kConcentrationComponent];
    }
}
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == kConcentrationComponent)
        return 90;
    return 205;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:54:23+00:00Added an answer on May 15, 2026 at 9:54 pm

    I didn’t look through all of your code; Instead, I’d recommend writing out the properties for numberOfComponents instead of @synthesize’ing them. Just get rid of your @synthesize, and make:

     - (int)numberOfComponents {
       return m_numberOfComponents;
    }
    

    and

     - (void)setNumberOfComponents(int aNumberOfComponents) {
       m_numberOfComponents = aNumberOfComponents;
    }
    

    Then, set a breakpoint in your setNumberOfComponents function, and you should be able to see whenever it’s getting called, so you can see what is going on. I hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIViewController that presents another UIViewController with a picker and returns four
I have an UIViewController (called MainViewController ) which presents modally a semi-transparent view (
I have a UIViewController called LaunchController that is launched in my iPhone app when
In my iPhone app I have a UIViewController that has a UITableView and another
So I have a UIViewController subclass called MyTabBarViewController that has a UIScrollView. Inside of
I have a UIViewController action that, when called, creates (totally in code) a UITableView
I have a class called BaseViewController that inherits from UIViewController and a class called
I have a root UIViewController which has a property called webView. WebView is a
I have a problem with data in UITableView. I have UIViewController, that contains UITableView
I have a UIViewController view as a subview/modal on top of another UIViewController view,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.