在 ViewDidLoad 中执行 Segue
Perform Segue in ViewDidLoad
如果应用程序是第一次加载,我正在尝试执行 segue。
我可以在调试器中看到我的打印消息,但是 Perform Segue 不工作。我没有收到任何错误。
有人可以告诉我怎么了吗?
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()。
在 viewDidLoad() 时,当前视图甚至还没有附加到 window,因此还无法继续。
您也可以使用不同的方法 - 根据 isFirstLaunch
boolean
将主 window 的 rootViewController
更改为您选择的视图控制器
UIApplication.shared.keyWindow?.rootViewController = setPasswordViewController
如果应用程序是第一次加载,我正在尝试执行 segue。 我可以在调试器中看到我的打印消息,但是 Perform Segue 不工作。我没有收到任何错误。 有人可以告诉我怎么了吗?
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()。
在 viewDidLoad() 时,当前视图甚至还没有附加到 window,因此还无法继续。
您也可以使用不同的方法 - 根据 isFirstLaunch
boolean
rootViewController
更改为您选择的视图控制器
UIApplication.shared.keyWindow?.rootViewController = setPasswordViewController