0

I am a newbie in iOS. What i want is that when a user gets successfully signed up, next view controller should appear. What I used for this purpose is prepareForSegue. User is getting signed in successfully. Debugging shows that prepareForSegue is also getting called but it's not showing the next screen. I also wanna send my "user" variable along with segue. Here is my code.

@IBAction func signUpTapped(_ sender: AnyObject!) {

        let password = "password"

        phone?.name = "phone_number"
        phone?.value = "######"

        email?.name = "email"
        email?.value = "######"

        attributes.append(phone!)
        attributes.append(email!)

        let userPool = appDelegate.userPool;

        userPool?.signUp("user7", password: password, userAttributes: attributes, validationData: nil).continueWith { (task) -> Any? in

            print("Successfully registered user: \(task.result!.user)");
            print("error: \(task.error)");
            self.user = task.result!.user
            self.performSegue(withIdentifier: "toConfirmationCodeScreen", sender: self)
            return nil
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if (segue.identifier == "toConfirmationCodeScreen") {
            let secondViewController = segue.destination as! ConfirmationCodeViewController
            let user = sender as! AWSCognitoIdentityUser
            secondViewController.user = self.user
        }
    }

Any help would be greatly appreciated.

P.s I am using Swift 3, Xcode 8.

kinza
  • 535
  • 11
  • 31
  • https://stackoverflow.com/a/41887007/5461400 @kinza – Harshal Valanda Sep 27 '17 at 04:50
  • 1
    Your authentication method probably runs asynchronously so you need to call self.performSegue from the main thread using `DispatchQueue.main.async { \\ your code to perform segue }` – Leo Dabus Sep 27 '17 at 04:55
  • @kinza You have written `self.performSegue(withIdentifier:sender)` method on button click action. In this case you need to create a segue from first viewcontroller to the second viewcontroller and not from the button to second viewcontroller. – Vineet Ravi Sep 27 '17 at 05:11
  • where (in the code) should i write this? @VineetRavi – kinza Sep 27 '17 at 05:12
  • @kinza You need to change the segue connection in storyboard.. You can do this by deleting your current segue and connect the first viewcontroller to the second view controller. – Vineet Ravi Sep 27 '17 at 05:14
  • 1
    @LeoDabus's comment worked for me. I was supposed to write self.segue in DispatchQueue.main.async { } Thanks alot! – kinza Sep 27 '17 at 07:18

2 Answers2

3

If you are calling self.performSegue(withIdentifier:sender) method on button click action, then in storyboard, you need to do this.. enter image description here

Not This... enter image description here

Vineet Ravi
  • 1,457
  • 2
  • 12
  • 26
1

You right click on button in storyboard and drag to the next view. Don't leave right click in between. You will get an option for Action Segue - Model,Show, popover, sheet. Just select show an you are done.

This is work fine then.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if (segue.identifier == "toConfirmationCodeScreen") {
        let secondViewController = segue.destination as! ConfirmationCodeViewController
        let user = sender as! AWSCognitoIdentityUser
        secondViewController.user = self.user
    }
}
Nupur Gupta
  • 305
  • 1
  • 12