Stop navigation controller from affecting other View controllers

I have an app that uses a navigation controller with three view controllers to step through user setup. So the first view will be Step 1, the second Step 2, and so on. And all this will be built into the navigation controller so that the user can go back and forth. However, once this customization is done and the user clicks the Finish button, the application will programmatically move to a different section of the storyboard that should be separate from the initial customization. I programmatically navigate to this section with the following code:

let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("welcomeViewController")
self.showViewController(vc as! UIViewController, sender: vc)

      

where "welcomeViewController" is the identifier of the post-configured ViewController. However, once that happens, the nav control bar still pops up at the top and you can still go back to the settings menu, which is not at all what I want. This is what I mean:

enter image description here

The user should not have access to the initial setup after setup. My storyboard looks like this:

enter image description here

The left four windows are for installation. The correct three windows are the "Main Menu" I want to create.

As you can see, there is no arrow between the last install controller and the input panel controller because I was doing it programmatically (as shown above). How do I get the navigation controller to stop affecting the rest of the View controllers?

+3


source to share


2 answers


This is because the segue under the hood is using the current one UINavigationController

, so it doesn't matter if you execute programmatically or in storyboards directly.

There are several ways to approach this. A very simple way to achieve this is to simply hide the back button in WelcomeViewController

with self.navigationItem.hidesBackButton = YES;

.

However, this doesn't really solve your problem if you really want to decouple these controllers and want to get rid of UINavigationController

who was in charge of your setup process. What I would recommend for this kind of situation is to use two different storyboards, one for your initial setup and one for the main body. Then you programmatically switch yours UIWindow

rootViewController

to a view controller from another storyboard. This can be done in a few lines of code and helps separate your concerns and better visualize an overview of the application flow.

Let me know in the comments if you need further explanation :)



Switching storyboards can be achieved with the following code:

UIStoryboard *main = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *welcomeViewController = [main instantiateViewControllerWithIdentifier:@"WelcomeViewController"];
UIWindow *mainApplicationWindow = [[[UIApplication sharedApplication] delegate] window];
mainApplicationWindow.rootViewController = welcomeViewController;

      

But if you want to instantiate an integer UITabBarController

that you are using in your main frontend, you will need to assign an ID (for example MainTabBarController

) to it in storyboards so that you can instantiate it using instantiateViewControllerWithIdentifier:

. So, instead of creating yours WelcomeViewController

, you go:

UIStoryboard *main = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *mainTabBarController = [main instantiateViewControllerWithIdentifier:@"MainTabBarController"];
UIWindow *mainApplicationWindow = [[[UIApplication sharedApplication] delegate] window];
mainApplicationWindow.rootViewController = mainTabBarController;

      

+2


source


A very simple solution to this problem is to make a transition from your last UIVieController, which you want to embed in a UINavigationController, to the one you want from a UINavigationController.

Step 1. Ctrl drag the yellow icon over the UIViewController (built in UINavigationController) to the UIViewController you want to move to (from the UINavigationController) and select "Present Modally"

Step 2. Give the segue id in the storyboard



Step 3. In your code, where you want to execute the segue, put;

performSegue(withIdentifier: "yourSegueID", sender: self)

      

This will do the trick and make your storyboard more readable.

0


source







All Articles