停止导航控制器影响其他视图控制器 [英] Stop Navigation Controller from affecting other View Controllers

查看:30
本文介绍了停止导航控制器影响其他视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用带有三个视图控制器的导航控制器来逐步进行用户设置.所以第一个视图将是第 1 步,第二个第 2 步等等.所有这些都将嵌入到导航控制器中,以便用户能够来回移动.但是,一旦此设置完成并且用户按下完成"按钮,应用程序就会以编程方式移动到故事板的另一部分,该部分应该与初始设置分开.我使用以下代码以编程方式转到此部分:

I have an app that uses a Navigation Controller with three View Controllers for step-by-step user setup. So the first View would be Step 1, second Step 2, and etc. And all this would be embedded in a Navigation Controller so the user would be able to go back and forth. However, once this setup is done and the user presses the 'Done' button, the app programmatically moves to another section of the storyboard which is supposed to be separate from the initial setup. I programmatically segue to this section with the following code:

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

其中welcomeViewController"是设置后 ViewController 的标识符.但是,一旦发生这种情况,导航控制器栏仍会出现在顶部,您仍然可以导航回设置菜单,这根本不是我想要的.这就是我的意思:

where "welcomeViewController" is the identifier for the post-setup ViewController. However, once this happens, the Navigation Controller Bar still appears on the top and you can still navigate back into the setup menu, which is not at all what I want. Here's what I mean:

用户在完成设置后应该无法访问初始设置.我的故事板看起来像这样:

The user shouldn't be able to access the initial setup after already setting up. My storyboard looks like this:

左侧四个框用于设置.右边三个框是我要创建的主菜单".

The left four boxes are for setup. The right three boxes is the 'Main Menu' I want to create.

如您所见,最后一个设置控制器和标签栏控制器之间没有箭头,因为我以编程方式执行了该转场(如上所示).如何让导航控制器停止影响其余的视图控制器?

As you can see, there's no arrow between the last setup Controller and the Tab Bar Controller because I performed that segue programmatically (as shown above). How do I get the Navigation Controller to stop affecting the rest of my View Controllers?

推荐答案

发生这种情况是因为引擎盖下的 segue 使用了当前的 UINavigationController,因此是否执行 segue 并不重要以编程方式或直接在故事板中.

This happens because a segue under the hood makes use of the current UINavigationController, so it doesn't matter if you perform the segue programmatically or in the storyboards directly.

有很多方法可以解决这个问题.实现它的一个非常简单的方法是使用 self.navigationItem.hidesBackButton = YES;WelcomeViewController 中简单地隐藏后退按钮.

There a number of ways how to approach this. A very easy way to achieve it is to simply hide the back button in your WelcomeViewController using self.navigationItem.hidesBackButton = YES;.

但是,如果您真的想要在这些视图控制器之间进行分离并且想要摆脱负责设置过程的 UINavigationController,这并不能真正解决您的问题.对于这种情况,我实际上建议使用两种不同的故事板,一种用于初始设置,一种用于主要部分.然后以编程方式将 UIWindowrootViewController 切换到另一个故事板中的视图控制器.这可以通过几行代码完成,有助于分离您的关注点并更好地直观了解您的应用流程.

However, this doesn't really solve your problem if you really want a separation between these view controllers and want to get rid of the UINavigationController that was responsible for your setup process. What I would actually recommend for that kind of situation is to use two different storyboards, one for your initial setup and one for the main part. Then you switch your UIWindow's rootViewController to a view controller from the other storyboard programmatically. This can be done in few lines of code and helps to separate your concerns and keep a better visual overview on your app's flow.

如果您需要进一步解释,请在评论中告诉我:)

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

可以使用以下代码实现切换故事板:

Switching storyboards can be achieved using the following code:

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

但是如果你想实例化你在你的主界面中使用的整个 UITabBarController,你必须在故事板中给它一个 ID(例如 MainTabBarController),这样您可以使用 instantiateViewControllerWithIdentifier: 实例化它.那么,您不必实例化您的 WelcomeViewController,而是:

But if you want to instantiate the whole UITabBarController you use in you r main interface, you'll have to give it an ID (e.g. MainTabBarController) in storyboards so that you can instantiate it using instantiateViewControllerWithIdentifier:. So then, instead of instantiating your WelcomeViewController, you'll go:

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

这篇关于停止导航控制器影响其他视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆