2

I have 2 UITextFields, one of them for Login and the another for the Password.

Only if the Login is "succesful", I want to perform the Segue with Push to another View Controller. But when I touch the button, directly the View push to the Another View without check the Condition.

In StoryBoard I drag from the UIButton to the View Controller I want to push, for creating the segue with push option.

Here is my code:

- (IBAction)buttonLogin:(id)sender {
    if (([self.textFieldLogin.text isEqualToString:@"User"]) && ([self.textFieldPassword.text isEqualToString:@"1515"])){

       [self performSegueWithIdentifier:@"SegueLogin" sender:self];

    }else{
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"User wrong"
                                                      message:@"Fill up again the user info"
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:@"Cancel", nil];
        [alert show];
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
santibernaldo
  • 825
  • 1
  • 11
  • 26

3 Answers3

13

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.

Like so:

enter image description here

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

You are using the wrong logical operator.

User &&instead of & inside your if statement.

For better understand of the difference between the two I recommend you to read this other stack overflow answer.

Community
  • 1
  • 1
Nuno Vinhas
  • 528
  • 4
  • 12
0

Swift 4 You can still link UIButton to the View Controller and create a Segue. If your login is successful then invoke self.performSegue within your closure. Something like this...

@IBAction func loginButtonPressed(_ sender: AnyObject) {
        authenticateUser(email: email.text!, password: password.text!)
}

func authenticateUser(email: String, password: String){
        # Building your loginUrl goes here

        Alamofire.request(loginUrl!,
                          method: .post,
                          parameters: nil,
                          encoding: JSONEncoding.default,
                          headers: headers)
            .validate()
            .responseJSON { (response:DataResponse<Any>) in
                switch(response.result) {
                case .success(_):

                    let apiResponse : JSON = JSON(response.result.value!)

                    print("Now Performing Segue on IBAction Pressed")
                    self.performSegue(withIdentifier: "goToDashboard", sender: self)
                   break

                case .failure(_):
                    print(response.result.error)
                    break
                }
            }
    }
onlinebaba
  • 81
  • 1
  • 2