ios - 阻止导航 Controller 影响其他 View Controller

标签 ios xcode swift uinavigationcontroller uinavigationbar

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

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

其中“welcomeViewController”是后设置 ViewController 的标识符。然而,一旦发生这种情况,导航 Controller 栏仍然出现在顶部,您仍然可以导航回设置菜单,这根本不是我想要的。这就是我的意思:

enter image description here

用户在完成设置后不应访问初始设置。我的 Storyboard如下所示:

enter image description here

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

如您所见,最后一个设置 Controller 和选项卡栏 Controller 之间没有箭头,因为我以编程方式执行了该 segue(如上所示)。如何让导航 Controller 停止影响我的其余 View Controller ?

最佳答案

发生这种情况是因为引擎盖下的 segue 使用了当前的 UINavigationController,因此无论您是通过编程方式还是直接在 Storyboard 中执行 segue 都没有关系。

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

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

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

可以使用以下代码实现切换 Storyboard:

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

但是如果你想实例化你在主界面中使用的整个UITabBarController,你必须在 Storyboard中给它一个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;

关于ios - 阻止导航 Controller 影响其他 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31310241/

相关文章:

ios - iPhone编程,加载plist数据,访问它

ios - 使用可变参数宏 Objective-C 时出错

iphone - 如何使 iPhone 应用程序中的 SQLite 数据库可用于桌面?

ios - 从 firebase 中获取 UI Image 与在 .text 中获取 UI Label 有何不同,图像获取会产生哪些潜在问题?

Xcode 6.3 没有助手结果

ios - fatal error 索引超出范围 swift 3 SpriteKit

iOS 蓝牙不发现设备

Swift-SceneKit-无法从 '.scn' 加载 'art.scnassets' 文件

iphone - 如何创建 session ?

ios - Objective-c:仅限横向