【问题标题】:Navigate through Scene Delegate浏览场景委托
【发布时间】:2021-11-14 08:58:15
【问题描述】:

我正在尝试获得我的故事板的蓝屏视图,但我对此完全陌生。我不明白如何在课程中导航以进入“蓝屏”,这给了我错误。

Story Board Image

最初的想法是通过SceneDelegate中的这个函数来传播变量“managedObjectContext”(CoreData)。

我得到的错误是:条件绑定的初始化程序必须具有 Optional 类型,而不是 '[UIViewController]'

This is my SceneDelegate Error

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    //guard let _ = (scene as? UIWindowScene) else { return }
    
    let mainViewController = window!.rootViewController as! UINavigationController
    
    if let navViewControllers = mainViewController.viewControllers { //error Initializer for conditional binding must have Optional type, not '[UIViewController]'
        let  controller = navViewControllers.first as! EditViewController
        controller.managedObjectContext = managedObjectContext
    }
    
    
    listenForFatalCoreDataNotifications()
    
    return
}

【问题讨论】:

  • 错误只是告诉你if let在这里毫无意义。摆脱它。

标签: ios swift storyboard uiscenedelegate


【解决方案1】:

我决定通过 segue 传播对象“managedObjectContext”。在这种情况下,它对我来说效果更好,因为我不太了解如何在场景应用程序中浏览故事板

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.identifier! {
    case addSegueIdentifier:
        guard let navController = segue.destination as? UINavigationController,
        let saveVC = navController.topViewController as? EditViewController else {
            return
        }
        saveVC.managedObjectContext = managedObjectContext
    default:
            break
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2020-02-20
    • 2020-01-30
    • 2020-01-30
    相关资源
    最近更新 更多