Reputation: 22715
I have a storyboard with a button in a Navigation View Control, this control has textfields in as well, which uses the storyboard segue push
.
However, I have some input checking, and I alert the user if any of the text fields are empty.
I can do this, BUT it alerts the user, and then switches to the other view.
Is there a way to cancel this push
if an if condition is not met in the button Pressed method?
N.B This push was created by the storyboard interface, not by code.
Upvotes: 15
Views: 10015
Reputation: 71
If buttonAction is the method that have to execute on button click you have to call two methods inside it 1. Method to check the conditions 2. Method to control the segue
- (IBAction)buttonAction:(id)sender
{
[self validateEnteredData];
[self shouldPerformSegueWithIdentifier:kSegueName sender:sender];
}
Your validation method will be looking as follows
-(void)validateEnteredData
{
if (self.usernameTextField.text.length==0 && self.passwordTextField.text.length==0)
{
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:kError message:kInvalidationMessage delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
return;
}
else if (![self.usernameTextField.text isEqualToString:@"admin"] && ![self.passwordTextField.text isEqualToString:@"pass"])
{
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:kError message:kLoginFailed delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
and segue can be controlled by the following code segment
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if ([identifier isEqualToString:kSegueName])
{
//Validation: Return NO if you don't want to execute the segue action
if (self.usernameTextField.text.length==0 && self.passwordTextField.text.length==0)
{
return NO;
}
else if (![self.usernameTextField.text isEqualToString:@"admin"] && ![self.passwordTextField.text isEqualToString:@"pass"])
{
return NO;
}
}
return YES;
}
Upvotes: 0
Reputation: 2874
The following can also be used and will work for different types of segues like push, unwind, etc. Name the segue from the Storyboard and use the code given here. Return NO for the condition if you do not want the particular segue to be performed and return YES by default. This method should be in the source view controller.
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:@"Segue_Name"]) {
NSLog(@"Segue Blocked");
//Put your validation code here and return YES or NO as needed
return NO;
}
return YES;
}
Upvotes: 28
Reputation: 3960
Don't create the segue by dragging from the button to the destination controller. Instead, just drag from the source controller to the destination controller to create a segue. Click on the segue and use the attribute inspector to specify the segue is a push and give the segue an identifier (goToDestination). Then connect the button to an IBAction in the source view controller. In the IBAction method do your checking and if you want to do the push add this line of code:
[self performSegueWithIdentifier: @"goToDestination" sender: self];
Upvotes: 33