Trenton Tyler
Trenton Tyler

Reputation: 1712

Parse signup going to next view controller if fields are missing

So I am trying to create a signup page, and I am running into some trouble. I want the user to HAVE to fill in all the information before they hit signup. After they hit the signup button, if they do not have all of the fields filled in they get an UIAlertView error for many different things. When I hit signup, it takes the user to the next page and then displays the UIAlertView, I do not want this to happen. I can't figure out what is happening. If the UIAlertView comes up if they do not have a username or email filled in I need the UIAlertView to display after they click the button and not go to the next page. I have a performSegue method in my code but it is performing first, and not last. Can anybody help me? I will include the screenshot of my storyboard as well.
The image shows the next view, as well as the segue Identifier

user.signUpInBackgroundWithBlock {
        (succeeded: Bool, error: NSError?) -> Void in
        if let error = error {
            let errorString = error.userInfo?["error"] as? NSString
            if fullname.isEmpty || email.isEmpty || username.isEmpty || password.isEmpty {
                var emptyFields:UIAlertView = UIAlertView(title: "Plese try again", message: "It looks like you forgot to fill out all the fields. Please make sure all the fields are filled out so we can create an account for you.", delegate: self, cancelButtonTitle: "Try again")

                emptyFields.show()
            } else if error.code == 203 {
                var takenEmail:UIAlertView = UIAlertView(title: "Please try again", message: "It looks like that email has already been taken, please try again.", delegate: self, cancelButtonTitle: "Try again")
            } else if error.code == 202 {
                var usernameTaken:UIAlertView = UIAlertView(title: "Please try again", message: "It looks like that username is already in use, please try again.", delegate: self, cancelButtonTitle: "Try again")
            }
        }
    }

Upvotes: 0

Views: 172

Answers (2)

Dawson Seibold
Dawson Seibold

Reputation: 145

There are a few ways to do this. The way I would do it is as follows.

Step 1. Ok you want to select your Sign Up View Controller. Step 1 Step 2. You are going to control click and drag from the Yellow View Controller Icon to the Second View Controller. In the Menu that appears choose the Show Or Present Modally Option.

Step 2 Step 3. Choose the Segue you just created and change its Identifier, In the right side panel to "signupSuccessful". Step 3

Step 4. You can set the code up like this.

if usernameTF.text.isEmpty || passwordTF.text.isEmpty {
            var emptyFields = UIAlertView()
            emptyFields.title = "Plese try again"
            emptyFields.message = "It looks like you forgot to fill out all the fields. Please make sure all the fields are filled out so we can create an account for you."
            emptyFields.addButtonWithTitle("Try Again!")
            emptyFields.show()
        }else if usernameTF.text == "ERROR CODE 203" {
            //do same thing with the alert view!
        }else if usernameTF.text == "ERROR CODE 202" {
            //Do another alert view here
            //You can keep adding the "else ifs" for any other error codes
        }
        //Here is where it will send to the second view when everything above is false!
        else {
            //Here we present the second view.
            self.performSegueWithIdentifier("signupSuccessful", sender: self)
        }

There you go. Here is a download of the project if you need to see the segue again, or any of the above code.

https://github.com/bobm77/ParseSegueExample

Upvotes: 2

deadbeef
deadbeef

Reputation: 5563

Implement the method func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool in your view controller and return false if you don't want the segue to happen.

Upvotes: 0

Related Questions