gpt4 book ai didi

ios - watchOS 2 : setting properties on initial Interface Controller

转载 作者:搜寻专家 更新时间:2023-10-30 22:18:59 24 4
gpt4 key购买 nike

从 watchOS 2 开始,我们有一个 ExtensionDelegate 对象,它类似于 UIApplicationDelegate(对应用程序生命周期事件作出 react )。

我想获得对第一个界面 Controller 对象的引用,该对象将在启动时显示,以在其上设置属性(例如,传入数据存储对象)。

根据docs , WKExtension 上的 rootInterfaceController 属性返回初始 Controller :

The root interface controller is located in the app’s main storyboard and has the Main Entry Point object associated with it. WatchKit displays the root interface controller at launch time, although the app can present a different interface controller before the launch sequence finishes.

所以我在 ExtensionDelegate 中尝试以下操作:

func applicationDidFinishLaunching() {
guard let initialController = WKExtension.sharedExtension().rootInterfaceController else {
return
}

initialController.dataStore = DataStore()
}

即使显示了正确的界面 Controller ,此时 rootInterfaceController 仍为 nil。有趣的是,如果我在我的界面 Controller 的 willActivate() 中查询相同的属性,则该属性设置正确。

在 iOS 应用程序中,您已经可以在 applicationDidFinishLaunching() 中获取 Root View Controller ,而且我认为它在 watchOS 上应该同样有效。

有没有办法在我的界面 Controller 显示之前(从外部)设置它的属性?这是错误吗?

非常感谢您的回答!

最佳答案

您可以将代码移至 applicationDidBecomeActive。

此页面描述 watch 应用程序的状态。调用 applicationDidFinishLaunching 时,应用处于非事件状态。

https://developer.apple.com/library/watchos/documentation/WatchKit/Reference/WKExtensionDelegate_protocol/index.html

关于ios - watchOS 2 : setting properties on initial Interface Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32824159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com