如何在 iOS 项目中控制多个导航控制器 [英] How to control multiple navigation controller in an iOS project

查看:20
本文介绍了如何在 iOS 项目中控制多个导航控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目结构如下.

最初当用户注册或尝试登录时,初始导航控制器应该可以工作,成功注册/登录后,用户应该被带到标签栏控制器的第一个选项卡.但我面临的问题是我在标签栏视图中看到了 2 个导航栏.有人可以指导我如何以正确的方式实现这一点.

Initially when the user is registering or trying to login the initial navigation controller should work and after successfully registering / loggin in the user should be taken to first tab of tab bar controller. But the issue that i am facing is that i am getting 2 navigation bars in the tab bar view. Can someone guide me how to implement this in the correct way.

提前致谢

推荐答案

在某些时候,我会假设应用程序能够确定用户是否登录,基于您必须设置所需的应用的根视图控制器.

At some point, I would assume that the app is able to determine whether the user loggedin or not, based on that you have to set the desired root view controller for the app.

对于这种情况,最好的地方是application(_:didFinishLaunchingWithOptions:) AppDelegate 文件中的方法:

For such a case, the best place to do that is application(_:didFinishLaunchingWithOptions:) method in the AppDelegate file:

告诉代理启动过程即将完成,应用程序几乎可以运行了.

Tells the delegate that the launch process is almost done and the app is almost ready to run.

为简单起见,假设您将 isLoggedin 布尔值保存在 UserDefault 中,因此可以这样实现:

For simplicity, let's say that you are saving isLoggedin boolean in the UserDefault, so it could be achieved like this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // the flag for determining whether the user loggedin or not
    let isLoggedin = UserDefaults.standard.bool(forKey: "K_isLoggedin")

    // the desired initial view controller (based on the value of `isLoggedin`)
    let initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: isLoggedin ? "TabbarIdentifier" : "FirstNavigationIdentifier")

    // setting the app rootViewController
    window?.rootViewController = initialViewController

    return true
}

请注意,TabbarIdentifier"代表故事板中的标签栏控制器,FirstNavigationIdentifier"代表故事板中的第一个导航视图控制器.

Note that "TabbarIdentifier" represents the tabbar controller at the storyboard and also "FirstNavigationIdentifier" represents the first navigation view controller at the storyboard.

如果您不知道如何设置视图控制器标识符,请查看这个答案应该会有所帮助.

if you are unaware of how to set the view controller identifier, checking this answer should help.

从技术上讲,设置所需的根视图控制器意味着设置rootViewController 到应用程序的主 window (AppDelegate窗口).

Technically speaking, setting the desired root view controller means setting the rootViewController to the main window of the app (AppDelegate window).

这篇关于如何在 iOS 项目中控制多个导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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