1

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()

            }
        }

    }
Ethan
  • 1,905
  • 2
  • 21
  • 50
user6155249
  • 129
  • 2
  • 12

1 Answers1

1

Try to segue in main thread

dispatch_async(dispatch_get_main_queue()) {
   self.performSegue(withIdentifier: "HomePageViewController", sender: self)
}
Chandan
  • 747
  • 7
  • 17
  • Thanks. Swift wouldn't allow me the exact you code since there has apparently been an update of the syntax. This is how it looks like now: DispatchQueue.main.asynchronously() { self.performSegue(withIdentifier: "HomePageViewController", sender: self) } The positive thing is that something happens, but now I get a thread 1 error: signal SIGARBT – user6155249 Jul 30 '16 at 18:09
  • `DispatchQueue.main.async { //self.performSegue(withIdentifier: "HomePageViewController", sender: self) }` – Chandan Jul 30 '16 at 18:17
  • Is there any log then share, might be issue of some outlet is not present in your code but in storyboard of HomePageViewController appearing as a yellow simple. Its just a guess of mine. – Chandan Jul 30 '16 at 18:21
  • Great, my pleasure. – Chandan Jul 30 '16 at 18:28