【发布时间】:2019-01-19 16:13:51
【问题描述】:
我有一些子系统需要在我的 swift 应用程序中初始化。不幸的是,视图控制器是在 AppDelegate 之前初始化的。我可以将只需要在启动时运行一次的代码放在哪里?
我尝试了 application(_:didFinishLaunchingWithOptions:) 但它是在我的 ViewController 初始化之后调用的。
在尝试快速调试 ViewController 并将其设置为初始视图控制器时会出现此问题。
复制步骤: 在下面的两个 *'d 行上放置一个断点。 MyTableViewController.init 在 AppDelegate .didFinishLaunchingWithOptions 之前被命中
class MyTableViewController: UITableViewController
required init?(coder aDecoder: NSCoder) {
*** print("init coder style")
super.init(coder: aDecoder)
}
... AppDelegate ...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
*** UIApplication.shared.isIdleTimerDisabled = true
return true
}
【问题讨论】:
-
可能是
application(_:didFinishLaunchingWithOptions:)? developer.apple.com/documentation/uikit/core_app/… -
它在我的初始 ViewController 中的 init 函数之后被调用。我将编辑我的问题。
-
“但它是在我的 ViewController init 之后调用的”它应该 NOT...顺便说一句,您能否详细说明“ViewController init”的含义是什么?!
application(_:didFinishLaunchingWithOptions:)应该在应用程序中的任何视图控制器初始化之前执行。 -
谢谢,我已经在我原来的问题中添加了一些细节,以准确显示正在发生的事情。
标签: ios swift initialization viewcontroller appdelegate