使用 storyBoardID 通过硬编码加载 ViewController
Load ViewController through hard code with storyBoardID
我正在尝试通过此代码加载视图:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
//let vc = self
let vc = storyboard.instantiateViewController(withIdentifier: "loginView") as! RegistrationViewController
self.present(vc, animated: true, completion: nil)
结果是:
2017-07-24 16:39:48.135 XXX [8689:286531] Warning: Attempt to present <XXX.RegistrationViewController: 0x7f8246206280> on <XXX.LaunchAppCheck: 0x7f8243d06bd0> whose view is not in the window hierarchy!
我已经在另一个问题中看到尝试使用 viewDidAppear()
,但问题仍然存在。
可能这条线困扰着你:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
这是在实例化另一个故事板,而不是您的实际故事板。
这是我使用的代码:
let vc: SettingsViewController = self.storyboard!.instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
self.present(vc, animated: true, completion: nil)
您不应该尝试与另一个 viewController 同时加载它,即 viewDidLoad(),因为此时它还不在堆栈中。
如果你真的需要同时呈现 2 viewControllers,请尝试使用延迟调用第二个:
vc = self.storyboard!.instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.present(vc, animated: true, completion: nil)
}
也许有帮助。
不要忘记在您的身份检查器中设置视图的 ID。
我正在尝试通过此代码加载视图:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
//let vc = self
let vc = storyboard.instantiateViewController(withIdentifier: "loginView") as! RegistrationViewController
self.present(vc, animated: true, completion: nil)
结果是:
2017-07-24 16:39:48.135 XXX [8689:286531] Warning: Attempt to present <XXX.RegistrationViewController: 0x7f8246206280> on <XXX.LaunchAppCheck: 0x7f8243d06bd0> whose view is not in the window hierarchy!
我已经在另一个问题中看到尝试使用 viewDidAppear()
,但问题仍然存在。
可能这条线困扰着你:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
这是在实例化另一个故事板,而不是您的实际故事板。
这是我使用的代码:
let vc: SettingsViewController = self.storyboard!.instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
self.present(vc, animated: true, completion: nil)
您不应该尝试与另一个 viewController 同时加载它,即 viewDidLoad(),因为此时它还不在堆栈中。
如果你真的需要同时呈现 2 viewControllers,请尝试使用延迟调用第二个:
vc = self.storyboard!.instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.present(vc, animated: true, completion: nil)
}
也许有帮助。
不要忘记在您的身份检查器中设置视图的 ID。