将导航控制器限制为一组 UIViewController

Limit Navigation Controller to a set of UIViewControllers

是否可以将 NavigationController 限制为一组特定的 UIViewController。看图,我想要导航控制器,但只用于 login/create 用户会话。登录后,我显然不希望用户能够返回(注销除外)。我怎样才能做到这一点?我想不通。

您可以在需要时更改导航控制器的堆栈,例如:

func logIn() {
   //Delete all presented view controllers up to a point
   navigationController.setViewControllers([], animated: false)

   //Create new view controller
   let viewController = ....
   navigationController.pushViewController(viewController, animated: true)
}

看看 setViewControllers,这可能会给您一些想法。

如果您想将以前的视图控制器留在堆栈中,并且只是禁止用户弹出它们,那么子类化 UINavigationController 并覆盖 func popViewController(animated: Bool) -> UIViewController?

可能是最好的解决方案

转到 Storyboard -> select NavigationController -> Attributes Inspector -> 取消选中 "Shows Navigation Bar" 属性

然后select与Login/SignUp和TabBarController的关系,删除

登录后,您可以将 TabBarController(或任何相关控制器)设置为 rootViewController。当应用程序启动时,您可以像这样在 AppDelegate.swift 文件中检查它,

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    FirebaseApp.configure()

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if Auth.auth().currentUser != nil {

        let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! TabBarController
        self.window?.rootViewController = tabBarController
        self.window?.makeKeyAndVisible()
    }
    else
    {
        let loginNavController = storyboard.instantiateViewController(withIdentifier: "LoginNavController") as! UINavigationController
        self.window?.rootViewController = loginNavController
        self.window?.makeKeyAndVisible()
    }
    return true
}