0

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:

  1. 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.

  2. I want view did load / unload and other messages to come the the sub view controllers that you would expect.

  3. 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
Robert
  • 37,670
  • 37
  • 171
  • 213

3 Answers3

1

Read this. Very elegant. http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited

Bourne
  • 10,094
  • 5
  • 24
  • 51
  • My favourite line of code from that: `self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[viewControllers arrayByPerformingSelector:@selector(title)]];` – Robert Mar 18 '11 at 12:59
0

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

Community
  • 1
  • 1
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
0

In your code above.

  1. 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.

Adarsh V C
  • 2,314
  • 1
  • 20
  • 37