【问题标题】:Can't set initial ViewController [duplicate]无法设置初始 ViewController [重复]
【发布时间】:2020-05-19 20:44:04
【问题描述】:

当我存储了访问令牌后,我想导航到主页,但它不起作用。它总是在启动时将我引导到 LoginViewController。

目前我正在使用此代码:

AppDelegate.swift

var window: UIWindow?

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

        let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")
        if accessToken != nil
        {
            let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let homePage = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
            self.window?.rootViewController = homePage                
        }

        return true
    }

项目的用户界面:故事板

斯威夫特:5

Xcode:11.4

目标部署:13.4

【问题讨论】:

标签: ios swift xcode


【解决方案1】:

试试这个

在我的应用程序中:它正在工作

let storyboard = UIStoryboard(name: "Main", bundle: nil)
var nvc: UINavigationController? = nil

nvc  = storyboard.instantiateViewController(withIdentifier: "NavRootVC") as? UINavigationController

let objCntrl : HomeVC = storyboard.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
nvc?.viewControllers = [objCntrl]
window?.rootViewController = nvc

【讨论】:

  • 它不起作用。我用HomeViewController 替换了NavRootVCHomeVC。为什么要使用两种不同的标识符?
  • 不要替换“NavRootVC”。只替换“HomeViewController”。然后再试一次。
  • Thread 1: Exception: "Storyboard (<UIStoryboard: 0x282f1cde0>) doesn't contain a view controller with identifier 'NavRootVC'"
  • 你的导航控制器类名是什么?
  • 我没有使用任何导航控制器... :)
【解决方案2】:

原因是你没有初始化window属性。 你只需要添加这两行

self.window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()

let homePage = mainStoryboard ...之后

【讨论】:

  • 我尝试添加这一行,但无论如何它不起作用。
  • 是的,检查我编辑的答案
  • 没有帮助。
【解决方案3】:

这可能会有所帮助:

   let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
   let homePage = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController

   let objNavigationController = UINavigationController(rootViewController: homePage ?? UIViewController())
   self.window = UIWindow(frame: UIScreen.main.bounds)
   window?.rootViewController = objNavigationController
   window?.makeKeyAndVisible()

【讨论】:

  • Cannot convert value of type 'HomeViewController.Type' to expected argument type 'UIViewController?'
  • 不应该是rootViewController: homePage吗?
  • 查看在此行完成的编辑更改(rootViewController: homePage ?? UIViewController())
  • 没有帮助。
【解决方案4】:

如果您的部署目标 > 13 那么你应该选择SceneDelegate。您可以在 SceneDelegate 中访问 window 属性。

在这个方法中设置你的 rootViewController

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

        guard let window  = (scene as? UIWindowScene) else { return }

}

此键应添加到您的 plist

【讨论】:

  • Application Scene Manifest 已经存在于 Info.plist 中,但我不知道如何将整个项目转换为 SwiftUI。
猜你喜欢
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-21
  • 2020-03-30
  • 2021-06-04
  • 2014-07-27
相关资源
最近更新 更多