简体   繁体   中英

Switching view controllers without navigation controller

I'm sure this has been asked countless times, and I've seen similar questions though the answer still eludes me.

I have an application with multiple view controllers and as a good view controller does its own task. However I find myself stuck in that I can't switch from one view controller to another. I've seen many people say "use a navigation controller" but this isn't what I want to use due to the unwanted view elements that are part and parcel to view controller.

I've done the following and have had limited success. The view controller is switched but the view does not load and I get an empty view instead:

- (IBAction)showLogin:(id)sender
{
    PPLoginViewController *login = [[PPLoginViewController alloc] initWithNibName:@"PPLoginViewController" bundle:nil];

    PPAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.window.rootViewController = login;

    [self.view insertSubview:login.view atIndex:0]; 
}

Using UINavigationController as a rootViewController is a good tone of creating iOS application.

As i understand unwanted view elements is a navigationBar? You can just hide it manually, setting:

[self.navigationController setNavigationBarHidden:YES];

And about your case, if you want to change you current viewController(targeting iOS 6), you can just present new one:

[self presentViewController:login animated:YES completion:nil];

or add child ( Here is nice example to add and remove a child) :

[self addChildViewController:login];

Why to set UINavigationController as a root?

1) First of all it makes your application visible viewcontrollers to be well structured. (Especially it is needed on iPhone). You can always get the stack and pop (or move) to any viewController you want.

2) Why I make always make navigation as a root one, because it makes the application more supportable , so to it will cost not so many code changes to add some features to the app.

If you create one (root) viewcontroller with a lot of children, or which presents other viewcontrolls, it will make your code really difficult to support, and make something like gode-object.

Listen to George, UINavigationController is the way to go. Your reasons for not wanting to use it are not valid.

However, the reason your code doesn't work might have to do with the unnecessary line after setting the rootViewController to the login vc.

Per Apple's documentation , setting rootViewController automatically sets the window's view to the view controller's view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM