【问题标题】:Swift - Instantiate View Controller in AppDelegateSwift - 在 AppDelegate 中实例化视图控制器
【发布时间】:2018-12-01 03:59:08
【问题描述】:

我试图让我的登录页面从AppDelegate.swift 的初始视图控制器上弹出,我正在使用
let authVC = storybaord.instantiateViewController(withIdentifier: "StartVC")
实例化它。但是,视图控制器链接到导航控制器,当我使用代码时,当我单击视图控制器的按钮时,它会使应用程序崩溃。代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()

    if Auth.auth().currentUser == nil {
        let storybaord = UIStoryboard(name: "Main", bundle: Bundle.main)
        let authVC = storybaord.instantiateViewController(withIdentifier: "StartVC")
        window?.makeKeyAndVisible()
        window?.rootViewController?.present(authVC, animated: false, completion: nil)
    }
    return true
}

Main.Storyboard 文件中,我在导航控制器的第一个视图控制器上有故事板 ID ("StartVC")。截图:http://prntscr.com/lp8fez | http://prntscr.com/lp8flq

以下是崩溃日志: Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'toName'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.

【问题讨论】:

  • "它使应用程序崩溃" 然后向我们展示崩溃日志。
  • 我认为你不能在启动时展示任何控制器!
  • 我编辑了帖子并添加了崩溃日志。

标签: ios swift storyboard


【解决方案1】:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
if Auth.auth().currentUser == nil {
    let storybaord = UIStoryboard(name: "Main", bundle: Bundle.main)
    let authVC = storybaord.instantiateViewController(withIdentifier: "StartVC")


    window?.rootViewController?= authVC

                 window?.makeKeyAndVisible()
}
return true
}

我已经使用了这段代码,它对我有用。如果需要更多帮助,请尝试告诉我。

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2021-11-23
    • 2015-03-24
    • 2015-01-01
    相关资源
    最近更新 更多