1

I have been creating my application in a primarily programatic approach and have been attempting to add a UISegmentedControl to the UINavigationControl Toolbar. I have the view created and shown and an action for when the UISegmentedControl is selected. The issue is that at any point I call selectedSegmentIndex, it returns a null value. Any ideas why?

    NSArray *segmentArray = [[NSArray alloc] initWithObjects:@"Factory Laods", @"User Loads", nil];

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems: segmentArray];

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];

    UIBarButtonItem *segmentedButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

NSArray *array = [[NSArray alloc] initWithObjects:flexibleSpace, segmentedButton, flexibleSpace, nil];
[self setToolbarItems:array];

-------- action method ------------

- (void) action:(id)sender {

UISegmentedControl *segment = (UISegmentedControl *) sender;
NSLog(@"Button %@", [segment selectedSegmentIndex]);

}

The flexibleSpace object is a UIBarButtonItem initialized to be just a flexible space to center out the UISegmentedControl. After this item is added I can add an NSLog statement and identify a null value for the selectedSegmentIndex and it's also null when the event is triggered and checked in the action method. Thanks!

1
  • How did you implement the action method? Jun 15, 2011 at 22:09

2 Answers 2

1

Your action method might be helpful to see but in your code above, you've included segmentedButton to the array which you set the toolbar items with but you created it as segmentedControl.

Could be a typo, or your issue!

4
  • This is the code I used to create a bar button item to add to the toolbar, hence the segmentedButton being added. do I not need to do this? UIBarButtonItem *segmentedButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; NSArray *array = [[NSArray alloc] initWithObjects:flexibleSpace, segmentedControl, flexibleSpace, nil]; [self setToolbarItems:array]; [delegate.navController setToolbarHidden:NO animated:YES];
    – Andrew
    Jun 16, 2011 at 4:36
  • I added more the rest of the code in my original question. but i don't know what I'm doing wrong still to get the null value returned.
    – Andrew
    Jun 16, 2011 at 5:31
  • Ah right, that explains that then. The problem looks to be what Jeremy alluded to in his answer. selectedSegmentIndex returns an int. If you modify the NSLog statement in your action method to NSLog(@"Button %d", [segmentedControl selectedSegmentIndex]); what happens?
    – deftangel
    Jun 16, 2011 at 11:24
  • HA! something so simple right? Thanks guys... I should have known better. Guess I just need a second pair of eyes.
    – Andrew
    Jun 17, 2011 at 2:57
1

selectedSegmentIndex returns an NSInteger, not an object. A NULL index is the index 0, i.e., the first segment is currently selected.

Also, this line leaks the item array:

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]
        initWithItems:
                [[NSArray alloc] initWithObjects: @"Item 1", @"Item 2", nil]];

An owning reference is returned by -initWithObjects: and no corresponding release follows. You can either use -arrayWithObjects: or assign the returned array to a temporary variable so you can release it after initializing your segmented control.

1
  • Thanks for the tip, I knew I was leaking, just haven't gotten to fixing it yet :P
    – Andrew
    Jun 16, 2011 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.