我正在开发一个 iOS 应用程序,但我没有 Storyboard,我做的是纯 swift 代码,当我尝试检查 MainViewController 中的身份验证时,我使用 perform 转到另一个 ViewController如果 token 在 MainViewController 中不存在显示按钮,我会遇到此警告并且它不会工作。
当我像下面的代码片段一样使用执行通过单击按钮转到另一个 ViewController 时,它工作得很好。
我已经看到我在这里问的标题的所有答案,但所有示例都有 Storyboard,所以它与我在这里的问题无关。
这是我尝试在我的应用中做的一个片段。
if defaults.string(forKey: Constants().userTokenKey) != nil
&& defaults.string(forKey: Constants().userTokenKey) != "" {
print("YOU ARE IN ELSE!")
let vc = SelectLocationOnMapViewController()
UIApplication.topViewController()?.present(vc, animated: true, completion: nil)
} else {
UIView.animate(withDuration: 1, animations: {
self.loginRegisterParentView.alpha = 1.0
})
setButtonActions()
}
最佳答案
问题就在这里
UIApplication.topViewController()?.present(vc, animated: true, completion: nil)
您似乎使用了尚未完全呈现的 rootVC 来呈现另一个 Vc ,您需要将此代码从 viewDidLoad
移动到 say viewWillAppear/viewDidAppear
关于ios - 如何解决此错误 "Warning:Attempt to present whose view is not in the window hirearchy"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56201901/