Search code examples
iphoneiosipaduiviewcontrolleruiwindow

How to use multiple iOS custom view controllers without a navigation controller


I am building an app that uses multiple types of screens--all which warrant their own custom view controllers. I am successfully switching between view controllers and their related views by reassigning the main window's rootViewController with a method in my app delegate like the following:

- (void)changeRootViewController:(NSString *)controllerName
{
    if (controllerName == @"book") {
        rootViewController = (UIViewController *)[[BookViewController alloc] init];
        [self.window setRootViewController:rootViewController];
    } else if (controllerName == @"something_else") {
        // Use a different VC as roowViewController
    }
}

The way that I am doing this seems like it just can't be best practice, however. I don't want to use a UINavigationController or a UITabBarController as the rootViewController, either. Is this the wrong way to be doing this, and if so, how should I be approaching this differently?

I thought this would have been covered somewhere, but (I feel as if) I've Googled the heck out of it, looked for related questions, etc. Sorry if I've missed something!


Solution

  • Its not a bad solution. You basically set one view as the root view. When you need another UIViewController you set another one. Just be careful for the leaks...

    • Create the rootViewController as property of the class with retain.
    • Before this:

    rootViewController = (UIViewController *)[[BookViewController alloc] init];

    Add this:

    if(rootViewController){
        self.rootViewController=nil;
    }
    

    }

    So you release the previous one.

    Edit 1: One thing: my explanation here is based on the fact that you don't want to use an UINavigationController.