IOS |无法更改应用程序委托中的根视图控制器 [英] IOS | Unable to change root view controller in app delegate

查看:23
本文介绍了IOS |无法更改应用程序委托中的根视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,如果用户登录,则必须更改其根视图控制器.假设用户已登录,如果用户未登录,我必须将标签栏控制器显示为主屏幕,我必须显示身份验证控制器.我的两个控制器都是故事板控制器.现在在我的应用程序委托中,我已经添加了以下代码

I am building an app whose root view controller must be changed if the user is logged in. Say if the user is logged in I must show a tab bar controller as the home screen if the user is not logged in, I must show an Authentication controller. Both my controllers are storyboard controllers. Now in my app delegate, I have put the following code

        window = UIWindow(frame: UIScreen.main.bounds)

        if UserDefaults.standard.bool(forKey: Constants.UserDefaultsKeys.isLoggedIn){
            initialViewController = storyboard.instantiateViewController(identifier: Constants.StoryBoards.homeViewController) as! TabController
        }else{
            initialViewController = storyboard.instantiateViewController(identifier: Constants.StoryBoards.authenticationController)
        }
        window?.rootViewController = initialViewController
        window?.makeKeyAndVisible()

根据代码,如果用户登录,必须显示TabController.但它没有显示.我试过调试和TabController>viewDidLoad 正在被调用,但我的 authenticationController 仍在显示,这可能是因为 authenticationController 被设置为故事板中的初始视图控制器.有人可以帮我找出问题

As per the code if the user is logged in ,TabController must be showed.But it is not being shown.I have tried debugging and TabController's viewDidLoad is being called but still my authenticationController is being shown and that is probably because authenticationController is set as the initial viewcontroller in the storyboard. Can someone help me figure out the issue

推荐答案

如果您只针对 iOS 13+,您唯一需要做的更改就是添加一行:

If you are targeting only iOS 13+, the only change you should need to make is to add one line:

    window?.rootViewController = initialViewController

    // add this line
    self.window = window

    window?.makeKeyAndVisible()

如果你想支持早期的 iO​​S 版本,这里有一个完整的 SceneDelegate/AppDelegate 实现:

If you want to support earlier iOS versions, here is a complete SceneDelegate / AppDelegate implementation:

SceneDelegate.swift

//
//  SceneDelegate.swift
//  Created by Don Mag on 3/27/20.
//

import UIKit

// entire class is iOS 13+
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        print("Scene Delegate willConnectTo", UserDefaults.standard.bool(forKey: "isLoggedIn"))

        guard let windowScene = (scene as? UIWindowScene) else { return }
        let window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window.windowScene = windowScene

        if UserDefaults.standard.bool(forKey: "isLoggedIn") {
            guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC") as? TabController else {
                fatalError("Could not instantiate HomeVC!")
            }
            window.rootViewController = vc
        } else {
            guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AuthVC") as? AuthViewController else {
                fatalError("Could not instantiate HomeVC!")
            }
            window.rootViewController = vc
        }

        self.window = window

        window.makeKeyAndVisible()
    }

}

AppDelegate.swift

//
//  AppDelegate.swift
//  Created by Don Mag on 3/27/20.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window : UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
            if #available(iOS 13, *) {
                // do only pure app launch stuff, not interface stuff
            } else {

                print("App Delegate didFinishLaunching... isLoggedIn:", UserDefaults.standard.bool(forKey: "isLoggedIn"))

                self.window = UIWindow()

                if UserDefaults.standard.bool(forKey: "isLoggedIn") {
                    guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC") as? TabController else {
                        fatalError("Could not instantiate HomeVC!")
                    }
                    window?.rootViewController = vc
                } else {
                    guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AuthVC") as? AuthViewController else {
                        fatalError("Could not instantiate HomeVC!")
                    }
                    window?.rootViewController = vc
                }

                window?.makeKeyAndVisible()

            }
            return true
    }

    // MARK: UISceneSession Lifecycle

    // iOS 13+ only
    @available(iOS 13.0, *)
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    // iOS 13+ only
    @available(iOS 13.0, *)
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    }

}

这篇关于IOS |无法更改应用程序委托中的根视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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