Courtney Stephenson
Courtney Stephenson

Reputation: 940

Switch Views Within UITabBar Tab Using Segmented Control

I am working on an app that allows two media galleries to be loaded into the same tab. They have separate XIB files and are loaded as such. However, once I switch views using the segmented control the UITabBar disappears.

This is how I am switching the views:

//Switch Views
- (IBAction)segmentSwitch:(id)sender
   {
   if([sender selectedSegmentIndex] == 0)
   {
      MediaViewController *vc = [[MediaViewController alloc] initWithNibName: @"MediaView" bundle:nil];
      vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
      [self presentModalViewController: vc animated: YES];
   }
   else if([sender selectedSegmentIndex] == 1)
   {
      MediaViewControllerTwo*v2 = [[MediaViewControllerTwo alloc] initWithNibName: @"MediaViewTwo" bundle:nil];
      v2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
      [self presentModalViewController: v2 animated: YES];
   }
}

Upvotes: 0

Views: 1433

Answers (2)

sergio
sergio

Reputation: 69047

The reason why your view disappear is that you are showing a modal view controller on top of it:

    [self presentModalViewController: v2 animated: YES];

As you know a modal view controller takes the whole display space for it.

What are you trying to accomplish with your segmented control?

EDIT: in order to change the current view displayed in the UITabBar, you have several options. One is simply adding a subview to the current view; in your view controller do:

[self.view addSubview: vc.view];
[vc viewWillAppear:NO];
[vc viewDidAppear:NO];

the calls to viewWillAppear and viewDidAppear are required, the framework will not call them for you in this case.

I assume that you segmented control is used to control a subarea of your view (i.e., your view has an upper part where the segmented control is and a lower part with some other content). In this case, I would simply try and replace the subview which lays below the segmented control. The technique is just the same.

After reading your comment, you could replace one table with the other one. Or better, have them both added as subview (full frame), and just hide one or the other according to the segmented control selection.

Upvotes: 2

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

You should not present them modally when using a UITabBarController. They are not the right tools. You should either switch the views attached to the view controller that is associated with the tab. How you would do it depends on how you got your tab bar modelled.

Upvotes: 0

Related Questions