Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]; 
}
like image 229
Marqueone Avatar asked Aug 25 '13 14:08

Marqueone


1 Answers

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.

like image 198
B.S. Avatar answered Oct 07 '22 02:10

B.S.