本文介绍了在ViewDidLoad中执行Segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果应用是第一次加载,我正在尝试执行segue.我可以在调试器中看到我的打印消息,但是Perform Segue无法正常工作.我没有任何错误.有人可以告诉我怎么了吗?
I`m trying to perform a segue if its the first time the app is loading.I can see my print message in the debugger, but the Perform Segue is not working. I don't get any errors.Can somebody please tell me whats wrong?
import UIKit
import LocalAuthentication
let isFirstLaunch = UserDefaults.isFirstLaunch()
extension UserDefaults {
// check for is first launch - only true on first invocation after app install, false on all further invocations
// Note: Store this value in AppDelegate if you have multiple places where you are checking for this flag
static func isFirstLaunch() -> Bool {
let hasBeenLaunchedBeforeFlag = "hasBeenLaunchedBeforeFlag"
let isFirstLaunch = !UserDefaults.standard.bool(forKey: hasBeenLaunchedBeforeFlag)
if (isFirstLaunch) {
UserDefaults.standard.set(true, forKey: hasBeenLaunchedBeforeFlag)
UserDefaults.standard.synchronize()
}
return isFirstLaunch
}
}
class loginVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if isFirstLaunch == false {
performSegue(withIdentifier: "setPassword", sender: self)
print("testFalse") }
else {
performSegue(withIdentifier: "setPassword", sender: self)
print("testTrue")}
// Do any additional setup after loading the view, typically from a nib.
}
推荐答案
您不能在viewDidLoad()中使用performSegue().将其移动到viewDidAppear().
You can't use performSegue() from within viewDidLoad(). Move it to viewDidAppear().
在viewDidLoad()时,当前视图甚至还没有附加到窗口,因此无法进行隔离.
At viewDidLoad() time, the current view isn't even attached to the window yet, so it's not possible to segue yet.
这篇关于在ViewDidLoad中执行Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!