简体   繁体   中英

how to perform a segue in segue destination view controller

In current view controller called OnBoarding, have button with action is this

@IBAction func jump(_ sender: Any) {
    if UserDefaults.standard.bool(forKey: "UserLog"){
        self.performSegue(withIdentifier: "goHome", sender: nil)
    }else{
        self.performSegue(withIdentifier: "goLogin", sender: nil)
    }
}

so if there is login user goes to home, but is not info then goes to login,( ViewController ), the login view controller have a segue to registerViewController, that have a button to return to login and after the registration process is returned to login screen, so now after the onboardingViewController action instead of go to login, the next screen should be register, I am looking for a way to command to loginviewController perform a segue from onBoardingViewController. tried this code but seem not work.

 if segue.identifier == "goLogin"{
        let vc = segue.destination as! ViewController
        vc.performSegue(withIdentifier: "register", sender: nil)
 }

I'm looking for the simple way to achieve this without having to make a direct segue between onboarding and register with involve in create a new segue or present the login screen and handle the different case where is presented or not.

It doesn't work because vc is not in a window hierarchy, when calling performSegue(withIdentifier:sender:) method.

Solution

Call performSegue(withIdentifier:sender:) method, once vc is on screen. To do it use a method of the UIViewController class perform(_:animated:completion:) .

if segue.identifier == "goLogin"{
    let vc = segue.destination as! ViewController
    present(vc, animated: true, completion: {
        vc.performSegue(withIdentifier: "register", sender: nil)
    })
}

a method trailing closures can be moved outside of brackets

if segue.identifier == "goLogin"{
    let vc = segue.destination as! ViewController
    present(vc, animated: true) {
        vc.performSegue(withIdentifier: "register", sender: nil)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM