user6155249
user6155249

Reputation: 129

Problems with initiating segue in swift

I am having some issues getting a segue to work properly.

Everything else is working, like saving the username and password to parse. But when the user presses either sign up or log in the segue doesn't work.

Here is my code:

import UIKit
import Parse

class ViewController: UIViewController {

// Declaring variables & functions
var signupMode = true

var acitivityIndicator = UIActivityIndicatorView()

func displayAlert (title: String, message: String) {

    let alertController = UIAlertController(title: title, message: title, preferredStyle: .alert)

    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

    self.present(alertController, animated: true, completion: nil)


}


// First view controller outlets/actions
@IBOutlet var signupButton: UIButton!
@IBOutlet var loginButton: UIButton!

@IBOutlet var emailTextField: UITextField!
@IBOutlet var passwordTextField: UITextField!

@IBAction func signupAction(_ sender: AnyObject) {

    if emailTextField.text == "" && passwordTextField.text == "" {

        displayAlert(title: "Error in form", message: "Please enter both an email and password")

    } else {

        acitivityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        acitivityIndicator.center = self.view.center
        acitivityIndicator.hidesWhenStopped = true
        acitivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        view.addSubview(acitivityIndicator)
        acitivityIndicator.startAnimating()
        UIApplication.shared().beginIgnoringInteractionEvents()

        if signupMode {

            // Sign Up

            let user = PFUser()

            user.username = emailTextField.text
            user.password = passwordTextField.text

            user.signUpInBackground(block: { (success, error) in

                self.acitivityIndicator.stopAnimating()
                UIApplication.shared().endIgnoringInteractionEvents()

                if error != nil {

                    var displayErrorMessage = "Please try again later."

                    if let errorMessage = error?.userInfo["error"] as? String {

                        displayErrorMessage = errorMessage

                    }

                    self.displayAlert(title: "Signup Error", message: displayErrorMessage)

                } else {

                    print("user signed up")

                    self.performSegue(withIdentifier: "HomePageViewController", sender: self)
                }


            })


        }


        }

    }


@IBAction func loginAction(_ sender: AnyObject) {

    acitivityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    acitivityIndicator.center = self.view.center
    acitivityIndicator.hidesWhenStopped = true
    acitivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
    view.addSubview(acitivityIndicator)
    acitivityIndicator.startAnimating()
    UIApplication.shared().beginIgnoringInteractionEvents()

    let userEmail = self.emailTextField.text

    let userPassword = self.passwordTextField.text

    PFUser.logInWithUsername(inBackground: userEmail!, password:userPassword!) {
        (user: PFUser?, error: NSError?) -> Void in
        if user != nil {

                self.performSegue(withIdentifier: "HomePageViewController", sender: self)

        } else {

            self.acitivityIndicator.stopAnimating()

            }
        }

    }

Upvotes: 1

Views: 68

Answers (1)

Chandan
Chandan

Reputation: 757

Try to segue in main thread

dispatch_async(dispatch_get_main_queue()) {
   self.performSegue(withIdentifier: "HomePageViewController", sender: self)
}

Upvotes: 1

Related Questions