winston
winston

Reputation: 3100

Swift segue not working?

My Swift segue is not working at all and isn't throwing any errors. The breakpoint shows me that the app lands on this line but nothing happens:

self.performSegueWithIdentifier("SignupSegue", sender: self)

The code block is a login form with Facebook:

if let accessToken: FBSDKAccessToken = FBSDKAccessToken.currentAccessToken() {
    PFFacebookUtils.logInInBackgroundWithAccessToken(result.token, block: {
        (user: PFUser ? , error : NSError ? ) - > Void in
        if user!.isNew {
            self.performSegueWithIdentifier("SignupSegue", sender: self)
        } else {
            self.navigateToInGame(true)
        }
    })
}

Here's the segue function it should call, but doesn't even get to it:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if (segue.identifier == "SignupSegue") {
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("SignUpViewController") 
            self.showViewController(vc, sender: self)
        }
    }

Any ideas?

Upvotes: 3

Views: 10857

Answers (6)

Daewizal Forshizal
Daewizal Forshizal

Reputation: 141

The closest I can get to overcome same problem for myself:

Made trigger var segueLogin : Bool = false with initialised value in the Class.

When PFFacebookUtils gets needed values for segue, change trigger to true:

PFFacebookUtils.logInInBackground(withReadPermissions: permissions) {
            (user: PFUser?, error: Error?) -> Void in
            if let user = user {
                if user.isNew {
                    print("User signed up and logged in through Facebook!")
                    self.segueLogin = true

                } else {
                    print("User logged in through Facebook!")
                    self.segueLogin = true
                }
            } else {
                print("Uh oh. The user cancelled the Facebook login.")
                self.loginCancelledLabel.alpha = 1
            }
        }

Then added code to viewDidAppear class. Realised it starts everytime PFFacebookUtils complete. So it checks if returned value is true and performs segue after successful PFFacebookUtils session:

override func viewDidAppear(_ animated: Bool) {
        if segueLogin == true {
            self.performSegue(withIdentifier: "segueSingup", sender: self)
        }
    }

Upvotes: 0

Ali Ihsan URAL
Ali Ihsan URAL

Reputation: 1974

You can try this ...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
    if (segue.identifier == "SignupSegue") {
        if let destination = segue.destination as? SignUpViewController {
         ...
        }

    }
}

Upvotes: 0

Darius Miliauskas
Darius Miliauskas

Reputation: 3514

Swift 3 solved:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if (segue.identifier == "SignupSegue") {
                DispatchQueue.main.async {
                    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                    let vc = storyboard.instantiateViewController(withIdentifier: "SignUpViewController")
                    self.show(vc, sender: self) 
                }
          }
    }

Upvotes: 0

Boopathy
Boopathy

Reputation: 415

Ok. I just tried it out. Hope you did all the things regarding StoryBoard Reference. Me too had the same issue with performSegueWithIdentifier.

Example: Let take two storyboard main and signup.

1) In main.storyboard create a storyboard reference. Set the storyboardId in the Storyboard Reference as signup and the referencedId as the storyboardId of the scene(viewController) which is in signup.storyboard. Look at this link for a clear picture Storyboard to Storyboard

2) Set the segue identifier between viewController and Storyboard Reference in main.storyboard

3) Since I faced the same problem with performSegueWithIdentifier, I replaced it with shouldPerformSegueWithIdentifier.

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    if(identifier == "segue_identifier"){
       // segue_identifier is the viewController and storyBoard Reference segue identifier.
        print("hello")
    }
    return true;
}

Let me know if you find any issues. It did work for me.

Upvotes: 2

Allen
Allen

Reputation: 1734

Generally, any UI updating has to be in main thread. I think the block for PFFacebookUtils.logInInBackgroundWithAccessToken is still in the background state in above situation. Maybe trigger the showViewController in dispatch_async(dispatch_get_main_queue(), {}) and see if there is any difference.

dispatch_async(dispatch_get_main_queue(), {
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("SignUpViewController")
    self.showViewController(vc, sender: self)
})

Upvotes: 3

Lumialxk
Lumialxk

Reputation: 6369

Performing a segue leads to present a new view controller.You don't need to and can't create and show view controller in prepareForSegue.It will look like:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if (segue.identifier == "SignupSegue") {

            let vc = segue.destinationViewController

        }
    }

Upvotes: 1

Related Questions