gpt4 book ai didi

ios - 在应用程序委托(delegate)中添加的 View Controller 未出现

转载 作者:行者123 更新时间:2023-11-28 15:37:29 24 4
gpt4 key购买 nike

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

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let auth: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Auth") as UIViewController

window?.rootViewController?.present(auth, animated: true, completion: nil)

return true
}

我收到错误...

Warning: Attempt to present on whose view is not in the window hierarchy!

我推测根 Controller 在应用生命周期的此时还没有正确配置。

我该怎么做?我想避免让根 Controller 必须检查它是否需要显示登录屏幕。

最佳答案

你可以这样做:

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if Settings.appSettings.authToken != nil {
self.showMainController()
}
NotificationCenter.default.addObserver(forName: .authorizationOperationDidSuccess,
object: nil, queue: nil) { (notification) in
self.showMainController()
}

return true
}

private func showMainController() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let controller: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Main") as UIViewController
if self.window == nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
}
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}

private func showAuthorizationController() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let controller: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Auth") as UIViewController
if self.window == nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
}
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}

登录成功

NotificationCenter.default.post(name: .authorizationOperationDidSuccess,
object: nil)

关于ios - 在应用程序委托(delegate)中添加的 View Controller 未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44131749/

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