在检查其他方法问题之前调用 shouldPerformSegueWithIdentifier

shouldPerformSegueWithIdentifier called before checking other method issue

我正在尝试进行 segue,但我有一个小问题,即我正在单击一个按钮,如果布尔值是 YES,它将 return YES in segue else return 不,但每次我都必须点击两次来检查文本字段,因为它首先传递给 shouldPerformSegueWithIdentifier 而它应该首先检查 IBAction。

请问我该如何解决这个问题?

- (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;
  }
}

您需要从按钮中的操作中删除 segue,将其移至视图控制器,然后在 IBAction 方法中调用 performSegueWithIdentifier,或者仅在 shouldPerformSegueWithIdentifier 并删除 IBAction 方法。

所以,要么 -

- (IBAction)search:(id)sender{

    if ([self.txtfld.text isEqual:@"test"]) {

       [self performSegueWithIdentifier:@"SearchSegue" sender:self];
    }

}

并摆脱 shouldPerformSegueWithIdentifier 或摆脱 IBAction 方法而只有 -

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {

   BOOL ret=YES;

   if ([identifier isEqualToString:@"SearchSegue"]) {
       if (![self.txtfld.text isEqual:@"test"]) {
          ret=NO;
       }
   }
   return ret;
}