问题
I'm trying to make a segue but I've a small issue which is that I'm clicking on a button and if the boolean is YES it will return YES in segue else return NO but everytime I have to click twice to check the textfield because it is passing first to shouldPerformSegueWithIdentifier while it should check the IBAction first.
Please how I can I fix this issue?
- (IBAction)search:(id)sender{
if ([_txtfld.text isEqual:@"test"]) {
push = YES; //Bolean
}
else {
push = NO; //Bolean
}
}
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:@"SearchSegue"] && push==YES) {
NSLog(@"Showed");
return YES;
}
else{
NSLog(@"Not showed");
return NO;
}
}
回答1:
You either need to remove the segue from the action in your button, move it to the view controller and then invoke performSegueWithIdentifier in your IBAction method, or just have the logic in shouldPerformSegueWithIdentifier and remove the IBAction method.
So, either -
- (IBAction)search:(id)sender{
if ([self.txtfld.text isEqual:@"test"]) {
[self performSegueWithIdentifier:@"SearchSegue" sender:self];
}
}
and get rid of shouldPerformSegueWithIdentifier or get rid of the IBAction method and just have -
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
BOOL ret=YES;
if ([identifier isEqualToString:@"SearchSegue"]) {
if (![self.txtfld.text isEqual:@"test"]) {
ret=NO;
}
}
return ret;
}
来源:https://stackoverflow.com/questions/27939361/shouldperformseguewithidentifier-called-before-checking-other-method-issue