Search code examples
objective-ciosuiviewcontrolleruiapplicationdelegate

Show ViewControllerA from ViewControllerB


I have a rootController and 5 contentControllers, each as its own class.

I want to be able to call the next content controller from the current content controller. For example, if I'm currently showing contentController1, I want a way to show contentController2.

It'd be ideal if I could add a short method to every controllers' implementation file that passed the number of the controller to be called to the actual method that loads and shows the new controller.

For example:

@implementation ContentController1

- (int) loadNextController {
  //take the 1 from ContentController1, add 1 to it, and pass it somewhere else
}

Then somewhere else (the root controller? the app delegate?) add the following method that then loads and shows the contentController based on the int sent from the (int) loadNextController method:

-(void) loadNextController: (int) nextController {
//init and show controller
}

If you could show me the code and, more importantly, where it goes, I would really appreciate it.

Trevor


Solution

  • It's not clear exactly how you want these view controllers to relate to each other. For example, you might want to push each one in turn onto a navigation controller's stack, so that the user always has the option of going back through the previous view controllers. Or, you might have a requirement that says that the user has to go through the five controllers in succession without the option to go back, in which case you'd probably set the window's rootViewController property to each controller when its time comes. Your decision will determine how you write the code that presents each controller's view. Read View Controller Programming Guide for details.

    The design you describe above has each view controller deciding which view controller to present next. That's often appropriate, especially where the view controllers form a hierarchy. From your description, though, it seems that it might be helpful to concentrate all the knowledge about the sequence in which controllers are presented in one object. Make that object the delegate of each controller, and have that object (possibly the application delegate) determine the order of the controllers. As an (untested) example, you might add this to your app delegate:

    -(void)application:(UIApplication*)app didFinishLaunchingWithOptions:(NSDictionary*)options
    {
        //...
        self.controllers = [NSArray arrayWithObjects:
                       [[[ViewControllerA alloc] initWithNibName:nil bundle:nil] autorelease],
                       [[[ViewControllerB alloc] initWithNibName:nil bundle:nil] autorelease],
                       //...
                       [[[ViewControllerN alloc] initWithNibName:nil bundle:nil] autorelease]];
    
        // Make self the delegate for each of the view controllers.
        [self.controllers setValue:self forKey:@"delegate"];
    
        [self.navigationController pushViewController:[self.controllers objectAtIndex:0] animated:NO];
    }
    
    -(void)viewControllerDidFinish:(UIViewController*)controller
    {
        NSUInteger index = [self.controllers indexOfObject:controller];
        NSUInteger nextIndex = index + 1;
    
        if (nextIndex < [self.controllers count]) {
            [self.navigationController pushViewController:[self.controllers objectAtIndex:nextIndex animated:YES];
        }
    }
    

    Then, whenever a view controller is ready to switch to the next controller, it can just call:

    [self.delegate viewControllerDidFinish:self];
    

    Again, the idea here is that the order of the view controllers in the array determines the order in which the controllers will be presented. I've used a nav controller here, but you don't have to. Also, you'll probably want to declare a protocol with your -viewControllerDidFinish:(UIViewController*)controller method, adopt that protocol in your app delegate (or whichever object manages the controllers), and have your controllers' delegate properties specify that protocol. That'll avoid any warnings about the delegate not implementing the method.