Swapping in a View Controller without using a UITabBar or Nav Controller
I have a segmented control in a View controller.
I want to load a different view into the middle when segments are tapped. Effectively making it a tab bar controller. (Q: why not just use a tab bar controller, A: I already have one)
Here are my requirements:
I want to use view controllers everywhere. One 'super view controller' to manage the segmented control. Two 'sub view controllers' to display the content when the segmented control is tapped.
I want view did load / unload and other messages to come the the sub view controllers that y开发者_JAVA技巧ou would expect.
I want a neat, elegant solution. Minimal hacking.
My current implementation fails on point 2:
- (IBAction)valueChanged:(id)sender {
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
{
ThirdVC* vc = [[ThirdVC alloc] initWithNibName:@"ThirdVC"
bundle:nil];
[self.centerView addSubview:vc.view];
}
break;
... etc
Read this. Very elegant. http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited
maybe my answer to Implementing my own navigation controller? will be helpful.
But I don't know if it's really elegant and non-hackish
In your code above.
- You are creating a new viewcontroller on each tap and not releasing it. This will cause memory leaks.
Solution.
You can create two instance variables of UIViewController(means:- Sub Viewcontrollers ) for your super view controller.
You can add and remove this sub viewcontollers to the super view controller according to the segment action.
You can listen to the view did load / unload message by using
-(void)viewWillAppear:(BOOL)animated
(void)viewWillDisappear:(BOOL)animated
methods of Sub View Controller.
Hope this will help.
精彩评论