阻止导航控制器影响其他视图控制器

时间:2015-07-09 06:58:48

标签: ios xcode swift uinavigationcontroller uinavigationbar

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

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

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

enter image description here

用户在设置后不应该能够访问初始设置。我的故事板看起来像这样:

enter image description here

左侧四个框用于设置。右边的三个框是我想要创建的“主菜单”。

如您所见,最后一个设置Controller和Tab Bar Controller之间没有箭头,因为我以编程方式执行了segue(如上所示)。如何让导航控制器停止影响我的其他视图控制器?

2 个答案:

答案 0 :(得分:2)

这是因为引擎盖下的segue使用了当前UINavigationController,因此如果您以编程方式或直接在故事板中执行segue并不重要。

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

但是,如果您真的希望在这些视图控制器之间进行分离并希望摆脱负责设置过程的UINavigationController,则这并不能解决您的问题。对于这种情况,我实际建议使用两个不同的故事板,一个用于初始设置,一个用于主要部分。然后,您以编程方式将UIWindow rootViewController从其他故事板切换到视图控制器。这可以通过几行代码完成,有助于区分您的问题,并对您的应用流程保持更好的视觉概览。

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

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

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,而是去:

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

答案 1 :(得分:0)

一个非常简单的解决方案就是解决这个问题,就是从你想要嵌入UINavigationController的最后一个UIVieController到你想要的UINavigationController。

第1步。 Ctrl从UIViewController(嵌入在UINavigationController中)顶部的黄色图标拖动到想要转换到UIVavigationController的UIViewController,然后选择"模拟呈现"

第2步。 在故事板中为segue提供标识符

第3步。 在您的代码中,您想要执行segue,put;

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

这样可以解决问题,让你的故事板更具可读性。