gpt4 book ai didi

Swift viewWillAppear 重定向

转载 作者:行者123 更新时间:2023-11-28 16:04:52 26 4
gpt4 key购买 nike

我正在使用“viewWillAppear”来检查 UserDefaults.standard,如果我有关于默认值的数据,我想重定向到仪表板 View 并退出登录。

当我正确登录时,应用程序正常重定向,但在有数据的情况下不会在 viewWillAppear 中重定向。

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

let defaults = UserDefaults.standard
if defaults.string(forKey: "username") != nil {
print("Exist")
print(defaults.string(forKey: "username")!)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "Dashboard")
self.present(vc, animated: true, completion: nil)
}

}

我在调试时遇到了这个错误。

2016-11-04 17:44:32.130024 RHiOS[1155:278240] Warning: Attempt to present <RHiOS.MenuViewController: 0x100f0d480> on <RHiOS.ViewController: 0x100e0b880> whose view is not in the window hierarchy!

如果有数据,我如何重定向到 Dash?谢谢。

最佳答案

您不能在 viewWillAppear 中呈现 ViewController。只需将这段代码放在 viewDidAppear 中,它就可以解决您的问题。
当 View 不可见时,它不在所提到的错误的窗口层次结构中。您还可以在 Storyboard中创建一个 segue 并改为调用 performSegue(withIdentifier:sender:)

关于Swift viewWillAppear 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433279/

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