简体   繁体   中英

Segue not being performed in a conditional statement

I'm currently trying to get a conditional statement to use performSegue(withIdentifier:sender:) . Currently, I receive no error but the segue is not performed. The project is using Facebooks SDK so loginButton() is called when the user logs in (this part works, the console prints "user is logged in" which confirms that SDK if functioning properly)

The identifier is spelled correctly within the storyboard

class LoginViewController: UIViewController, FBSDKLoginButtonDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()

        let loginButton = FBSDKLoginButton()
        loginButton.frame = CGRect(x: 15, y: view.frame.maxY - 50, width: view.frame.width - 32, height: 32)
        view.addSubview(loginButton)

        loginButton.delegate = self
    }

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!)
    {
        if error != nil
        {
            print(error)
            return
        }
        else
        {
            print("user is logged in")
        }
        performSegue(withIdentifier: "postToView", sender: self)

    }

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!)
    {
        print("logged out")
    }

}

This is what the storyboard looks like 故事板

How can I get it to perform the segue once the conditional is true?

Replace the call to segue in loginButton() with this.

DispatchQueue.main.async {
    [unowned self] in
    self.performSegueWithIdentifier("postToView", sender: self)
}

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