Switch view controllers without a navigation controller

I am sure this has been asked countless times, and I have seen similar questions, although the answer still eludes me.

I have an application with several view controllers, and as a good view controller, its own job is being done. However, I found myself stuck in the fact that I cannot switch from one view controller to another. I have seen many people say “use a navigation controller”, but that’s not what I want to use because of the unwanted presentation elements that are part and parcel for viewing the controller.

I did the following and had limited success. The view controller switches, but the view does not load, and instead I get an empty view:

- (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]; } 
+4
source share
2 answers

Using the UINavigationController as the rootViewController is a good way to create an iOS application.

How do I understand the elements of an unwanted view - is it a navigationBar? You can simply hide it manually by setting:

 [self.navigationController setNavigationBarHidden:YES]; 

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

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

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

 [self addChildViewController:login]; 

Why set UINavigationController as root?

1) First of all, it makes your application visible viewcontrollers well structured. (This is especially necessary on the iPhone). You can always get the stack and pop (or move) to any desired viewController.

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

If you create one (root) viewcontroller with a lot of children or imagine other viewcontrolls, this will make your code very difficult to support and will make something like a gode object.

+12
source

Listen to George, the UINavigationController is the way to go. Your reasons do not want to use it invalid.

However, the reason your code doesn't work may be related to an unnecessary line after setting rootViewController to vc input.

Per Apple Documentation , installing rootViewController automatically sets the window view to the view controller's view.

+1
source

Source: https://habr.com/ru/post/1498842/


All Articles