shouldPerformSegueWithIdentifier called before checking other method issue

限于喜欢 提交于 2019-12-24 10:49:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!