Dejell
Dejell

Reputation: 14317

segue transition with condition storyboard

I am new to storyboard and xcode (using 4.4)

I would like to implement a login condition, that if accomplished - the segue would work. Otherwise, the user should stay on the same view.

I created 2 UIView: ViewController and Bar Controller.

I also created a segue from ViewController to Bar Controller and set its identifier to loginSegue as modal.

In LoginViewController.m I added the following:

- (IBAction)login:(id)sender {
    self.email = self.emailText.text;
    self.password = self.passwordText.text;
   if ([self.email isEqualToString:@"O"] && [self.password isEqualToString:@"O"])
       [self performSegueWithIdentifier:@"LoginSegue" sender:sender];
    else
        [passwordText setText:@""];   
}

When I debug the code - I see that the if skips to else in case of email and password that to not equal to O, however the segue (transition to the new view) still happens.

How can I prevent the user to be transferred to the new view?

BTW, if I remove the if else condition - the user is being transferred to the next view automatically. It seems to me that it was configured somewhere when I drew the segue from one view to the other.

Upvotes: 1

Views: 1906

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130193

Delete the blank line in between your if and [self perform...]

Additionally, you may want to consider keeping your if-else statements in braces to keep this problem from happening again:

if ([self.email isEqualToString:@"O"] && [self.password isEqualToString:@"O"]){
    [self performSegueWithIdentifier:@"LoginSegue" sender:sender];
}else{
    [passwordText setText:@""];
}

If this doesn't solve the problem then be sure to varify that the segue is attached to the two controllers and not directly to a button.

Upvotes: 6

Related Questions