Perform Segue in ViewDidLoad [duplicate]
Asked Answered
R

2

13

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.




    }
Randallrandan answered 12/8, 2017 at 18:54 Comment(6)
what prints in the log?Bandog
testTrue for the first launch and testFalse for the others?Bandog
Correct. The first line in the log is testFalseRandallrandan
is override func prepare(for segue: UIStoryboardSegue, sender: Any?) { fired?Bandog
show where is segue defined? Did you try to move it to viewDidAppear for exapmle?Sleeper
Moved it to ViewDidAppear and that fixed the problem. Thank You Salsores and SmartcatRandallrandan
T
43

You can't use performSegue() from within viewDidLoad(). Move it to viewDidAppear().

At viewDidLoad() time, the current view isn't even attached to the window yet, so it's not possible to segue yet.

Thibeault answered 12/8, 2017 at 19:22 Comment(4)
Thank you Smartcat. Moved the code to ViewDidAppear() and it works fine now!Randallrandan
Is it okay to call it from viewWillAppear do you know? It's testing okay for me but I'm worried about outlier cases or low-performance devices...Gove
@Gove I've successfully done so in some of my production code, but others have had trouble. See this SO for example. Maybe it depends on if there's a transition animation to be used? If you spot something definitive from Apple, please post here.Thibeault
@Thibeault you literally saved me a headache. Worst part is that I was really close, but I was approaching the problem in the wrong way. I was trying to run performSegue inside viewWillAppear. Just moving it to viewDidAppear fixed everything.Microdot
B
1

You can also use a different approach - change the main window's rootViewController to the view controller of your choice depending on isFirstLaunchboolean

UIApplication.shared.keyWindow?.rootViewController = setPasswordViewController

Babe answered 19/11, 2017 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.