I am currently adding a UISegmentedControl
to the toolbar in the navigation controller programmatically (as seen below).
This approach works fine, I have my UISegmentedControl
, it fires the selector that I have setup no problems.
Problem is - I would like to use the selectedIndex
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 selectedIndex
.
In my travels I have been consulting the 'Top Songs' example code provided by Apple.
In this code they build up their interface via UISegmentedControl
object in the view controller and IB. In doing so they can access the UISegmentedControl
's selectedIndex
. I am adding mine programmactically and do not have this freedom.
'SHOULD' I have a UISegmentedControl
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 UIToolBar
?
I'm clearly missing something basic. Any assistance is always gr开发者_开发百科eatly appreciated :)
[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];
You need to have tabs
defined in your .h file -
@interface YourViewController : UIViewController
....
UISementedControl *tabs;
....
@end
....
@property (nonatomic, retain) UISegmentedControl *tabs;
Then, after the [tabs release];
line, you should still be able to access the object, as it is a retained property - access the selectedItemIndex
as normal.
精彩评论