views:

193

answers:

1

Hi!

I am currently adding a UISegmentedcontrol to the toolbar in the navigation controller programactically (As 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 viewController and IB. In doing so they can access the UISegmentedControl selectedIndex. I am adding mine programmactically and do not have this freedom.

'SHOULD' I have a UISegmentedControl defined in my viewController? 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 navigationcontrol uitoolbar?

I'm clearly missing something basic. Any assistance is always greatly 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];"

A: 

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.

jrtc27
You meant `@interface`...
Michael Kessler
Of course. Sorry. My bad. Too quick to post ;)
jrtc27
In addition, @Lance should use `self.tabs = [[UISegmentedControl alloc] initWithItems:tabitems];` (important not to forget the "self." - otherwise it won't use the property setter and won't retain the instance...).
Michael Kessler
Many thanks to all!! I figured out the self.tabs business on my travels but thanks for the update!
Lance
Can you mark as answer please the if it was the answer? Many thanks
jrtc27
How do I go about that sorry?
Lance
Below the reputation increase/decrease buttons, there is a hollow tick. If you click it, it goes green, and shows that you have accepted my answer as the answer to your question. It gives me +15 rep and you +2.
jrtc27
Done, many thanks :)
Lance
For anyone trying to plug together the idea of a UISegmentedControl in the NavigationController.toolbar, the code quoted above is performed in the viewDidLoad();
Lance
No problem - glad I could help ;)
jrtc27