Matan Touti
Matan Touti

Reputation: 191

How to move between screens with a condition (if...)

Suppose i have a Register Class, and by a (UIButton) "Go" button i check all of the fields that the user entered, all of the fields are UITextField. I made a action for that "Go" button in the Register class to move to the next ViewController by a push, also in the action function i check the user input (for example bad email x@xxx) and if the user entered a bad input i dont want to move to the new screen. So, to make a long story short, My question is how i make a condition to move between the screens. "if the user entered a correct input so make the move... else stay" I dont want to make another button to check the correctness of the form and then, first the user will press the check button then the second button to send the form.

Thank you!

Upvotes: 1

Views: 253

Answers (2)

user1851650
user1851650

Reputation:

The easiest way to do this is to use a BOOL. You could for example set this BOOL to false within the viewDidLoad method. If all your checks you do with the input data are passed correctly you set this BOOL to true. Now you can use this value either to push the next view controller or not to push it. For example

if (theBOOL) {
    // push the next view controller
} else {
    // display a message on the current view controller to recheck the input data
}

Upvotes: 1

aahsanali
aahsanali

Reputation: 3547

-(BOOL) validateField:(UITextField *)field{

// perform your validation and return YES or NO

return YES;
}

in your button action

BOOL validated = NO;

if ([self validateField:textfield1]){

validated = YES;.

}else{
return; // or your error message
}
if ([self validateField:textfield2]){

validated = YES;
}else{

return;// or your error message

}

and so on..

if (validated){
[self pushViewController:yourcontroller]
}

Upvotes: 1

Related Questions