13

I have a single view application which connects to parse and verifies user log in information and updates a UILabel saying "Yay, You've logged in sucessfully" - Now I want it to navigate to a new view (Which will be the main part of the app).

I have dragged a new view controller onto the storyboard and Ctrl-Drag the button and linked it to the new controller with show. However, the moment I load the app and click the button it goes straight to the new view. I need it to only go there if the right part of the if-else statement is triggered.

Does this make sense? Thank for any help guys. much appreciated.

EDIT

The if statement is:

if usrEntered != "" && pwdEntered != "" {
        PFUser.logInWithUsernameInBackground(usrEntered, password:pwdEntered) {
            (user: PFUser!, error: NSError!) -> Void in
            if user != nil {
                self.messageLabel.text = "You have logged in";
            } else {
                self.messageLabel.text = "You are not registered";
            }
        }
    }

and its located in the ViewController.swift file

DannieCoderBoi
  • 728
  • 2
  • 12
  • 31
  • What is your if-else statement, and where is it placed? – A O Nov 23 '14 at 22:17
  • @MattyAyOh added to original question – DannieCoderBoi Nov 23 '14 at 22:18
  • the bit that is: self.messageLabel.text = "You have logged in"; - I want it to redirect to a new view – DannieCoderBoi Nov 23 '14 at 22:18
  • possible duplicate of [Xcode 6.1 button with segue](http://stackoverflow.com/questions/26799433/xcode-6-1-button-with-segue) – Paulw11 Nov 23 '14 at 22:20
  • When is this if statement being hit? What method is it living in your view controller implementation? If you create a new project, add another view controller in the interface builder, add the button to the root view controller, and bind it to `show` the new view controller-- it works There must be something else happening that is forcing your new view controller to show... – A O Nov 23 '14 at 22:32

1 Answers1

55

First off as I explain in this answer, you need to drag the segue from the overall UIViewController to the next UIViewController, i.e. you shouldn't specifically connect the UIButton (or any IBOutlet for that matter) to the next UIViewController if the transition's conditional:

storyboard segue

You'll also need to assign an identifier to the segue. To do so, you can select the segue arrow then type in an identifier within the right-hand panel:

segue identifier

Then to perform the actual segue, use the performSegueWithIdentifier function within your conditional, like so:

if user != nil {
    self.messageLabel.text = "You have logged in";
    self.performSegueWithIdentifier("segueIdentifier", sender: self)
} else {
    self.messageLabel.text = "You are not registered";
}

where "segueIdentifier" is the identifier you've assigned to your segue within the storyboard.

Community
  • 1
  • 1
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128