Here is the setup of my iPhone App:
I have a UITabBarController that has 4 View Controllers (1 UINavigationController & 3 UIViewControllers).
Onload my app is defaulted to the UINavigationController, where there is a grouped UITableView which gives two navigation options, when the user hits the first option the relevant UITableViewController (Contains a list of locations) is pushed onto the stack.
What I would like to happen on screen is to have a UISegmentedControl that has two options, a "List View" (which the user sees by default when the UIViewController gets pushed) ans a "Map View" that will allow the relevant locations to be represented on the map.
I use the follwing code to create the UISegmentedControl on the NavigationBar:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentItems];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[[self navigationItem] setTitleView:segmentedControl];
[segmentedControl release];
And this is the method that the UISegmented control will call when changed:
- (void) segmentAction:(id)sender
{
UISegmentedControl *segmentControl = sender;
if([segmentControl selectedSegmentIndex] == 0)
{
NSLog(@"LIST VIEW CHOSEN!");
}
else
{
NSLog(@"MAP VIEW CHOSEN!");
}
}
So basically my question is what is the correct/best way to implement what I am trying to achieve, I dont want to push the controller onto the navigation stack, I just want to toggle it in place (possibly with animation). All interfaces are built in IB.
One way i tried to do it, that worked but didn't feel like it was the correct was by creating two separate UIViews in my nib file (One for the List View and one one for the Map view), and then using setView: appropriately.. but then I thought shouldn't each of these views have there own controller and/or own nib?
- (void) segmentAction:(id)sender
{
UISegmentedControl *segmentControl = sender;
if([segmentControl selectedSegmentIndex] == 0)
{
NSLog(@"LIST VIEW CHOSEN!");
[self setView:listView]; //Declared as an UIView IBOutlet
}
else
{
NSLog(@"MAP VIEW CHOSEN!");
[self setView:mapView]; //Declared as an UIView IBOutlet
}
}
Some semi related examples I have come across use "removeFromSuperview" and "addSubview" and this has somewhat confued me, i'm relatively new to Cocoa Touch development, so any help getting my head around this would be appreciated.
Thanks