Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing UISegmentedcontrol selectedIndex - when contained in a UIButton contained in the UINavigationController.toolbar
    text
    copied!<p>I am currently adding a <code>UISegmentedControl</code> to the toolbar in the navigation controller programmatically (as seen below).</p> <p>This approach works fine, I have my <code>UISegmentedControl</code>, it fires the selector that I have setup no problems.</p> <p>Problem is - I would like to use the <code>selectedIndex</code> of this control in order to query my data model and present a different view of data for each 'segment' - but I am having trouble getting the <code>selectedIndex</code>.</p> <p>In my travels I have been consulting the 'Top Songs' example code provided by Apple. In this code they build up their interface via <code>UISegmentedControl</code> object in the view controller and IB. In doing so they can access the <code>UISegmentedControl</code>'s <code>selectedIndex</code>. I am adding mine programmactically and do not have this freedom.</p> <p>'SHOULD' I have a <code>UISegmentedControl</code> defined in my view controller? If so, if I want to continue building my menu programmactically, how do I go about accessing the information from the control buried within the navigation controller's <code>UIToolBar</code>?</p> <p>I'm clearly missing something basic. Any assistance is always greatly appreciated :)</p> <pre><code>[self.navigationController setToolbarHidden:NO]; // Set up the edit and add buttons. self.navigationItem.leftBarButtonItem = self.editButtonItem; UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)]; self.navigationItem.rightBarButtonItem = addButton; [addButton release]; NSArray *tabitems = [NSArray arrayWithObjects:@"ONE", @"TWO", @"THREE", @"FOUR", nil]; UISegmentedControl *tabs = [[UISegmentedControl alloc] initWithItems:tabitems]; [tabs addTarget:self action:@selector(pickedSegment:) forControlEvents:UIControlEventValueChanged]; tabs.segmentedControlStyle = UISegmentedControlStyleBar; tabs.frame = CGRectMake(60, 8, 180, 30); tabs.selectedSegmentIndex = 0; //[self.navigationController.navigationBar addSubview:tabs]; [self.navigationController.toolbar addSubview:tabs]; [tabs release]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload