1

I'm trying to implement the code described in this tutorial to switch UIViewControllers using an UISegmentedControl. The tutorial uses didFinishLaunchingWithOptions: to set everything up and show the first view:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSArray * viewControllers = [self segmentViewControllers];

    UINavigationController * navigationController = [[UINavigationController alloc] init];
    self.segmentsController = [[SegmentsController alloc] initWithNavigationController:navigationController viewControllers:viewControllers];

    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[viewControllers arrayByPerformingSelector:@selector(title)]];

    [self.segmentedControl addTarget:self.segmentsController
                          action:@selector(indexDidChangeForSegmentedControl:)
                forControlEvents:UIControlEventValueChanged];

    [self firstUserExperience];

    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

However, in my app, I'd like to call this from didSelectRowAtIndexPath: inside a UITableViewController, but I'm not sure how to do that. So the user selects a row, and a new view appears where I can switch between views using a UISegmentedControl at the top.

I guess the line that needs to change is [window addSubview:navigationController.view];, all the rest can be the same.

What would be the equivalent if I call that code from my UITableViewController instead of from the AppDelegate as in the tutorial ?

(This is not a duplicate of Switching ViewControllers with UISegmentedControl in iOS5, since I'd like to use the example from the tutorial and that question deals with how to set it up using storyboards.)

Community
  • 1
  • 1
koen
  • 5,383
  • 7
  • 50
  • 89

2 Answers2

3

While I don't think this is a nice solution (I would prefer creating a separate view controller, with the segmented control, and implement the switching logic there), the answer is yes: you can call this piece of code from your table view controller, adding the navigation controller's view as its subview, like so:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray * viewControllers = [self segmentViewControllers];
    
    UINavigationController * navigationController = [[UINavigationController alloc] init];
    self.segmentsController = [[SegmentsController alloc] initWithNavigationController:navigationController viewControllers:viewControllers];
    
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[viewControllers arrayByPerformingSelector:@selector(title)]];
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    
    [self.segmentedControl addTarget:self.segmentsController
                              action:@selector(indexDidChangeForSegmentedControl:)
                    forControlEvents:UIControlEventValueChanged];
    [self firstUserExperience];
    [self.view addSubview:navigationController.view];
    
}

I have downloaded the example you referred to (quite outdated by the way), and included it in a sample project. so you can check it out. All you need to do is add a new item, then select a row to invoke the relevant code piece.

Check the code here

UPDATE:

I have added a second example which uses a separate view controller, check it out if you want: Link

Community
  • 1
  • 1
József Vesza
  • 4,775
  • 3
  • 27
  • 42
  • I like the second approach. The only thing is that I see a black glow under the navigationbar, but that could be something in my code that is causing that, since i don't see it in your example. – koen Apr 20 '15 at 03:50
  • I just threw the example code into the master-data app template in Xcode, so I didn't really think about it. You can try to use the built-in view debugger to examine your layout at runtime. – József Vesza Apr 20 '15 at 05:35
  • Adding `self.navigationController.navigationBar.translucent = NO;` seems to solve this. – koen Apr 20 '15 at 11:10
1

Use the container view controller. The tutorial you're referring to was a hack used prior to container view controllers. The author should really pull that thing down.

SmileBot
  • 19,393
  • 7
  • 65
  • 62