简体   繁体   English

在alertview显示时,请勿连接segue

[英]Don't connect segue when alertview shows

I got a push segue connected in storyboards and then I'm using the prepareForSegue method like this: 我在故事板中连接了一个push segue然后我正在使用这样的prepareForSegue方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"start"] && self.name.textField.text == nil)
    {


        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Test"
                                                          message:@"Test
                                                         delegate:self
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];

    }
}

What I want to do is that if the "if" is true, there pops an alert box and preforms the segue, but I just want to show the alert box, and I don't want to preform the segue. 我想要做的是,如果“if”为真,则弹出一个警告框并预先形成segue,但我只想显示警告框,我不想预先形成segue。 How can I solve this? 我怎么解决这个问题?

prepareForSegue:sender: is called when the segue is about to be performed. prepareForSegue:sender:在即将执行segue时调用。 You cannot cancel it at that point. 你不能在那时取消它。

The method you should be overriding if you want to be able to cancel a segue is shouldPerformSegueWithIdentifier:sender: . 如果你想取消segue,你应该重写的方法是shouldPerformSegueWithIdentifier:sender: .

I think you need to reevaluate your logic here and remove the conditional from prepareForSegue 我认为你需要在这里重新评估你的逻辑并从prepareForSegue删除条件

Using UIAlertDelegate 使用UIAlertDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"OK"]) {
      [self performSegueWithIdentifier:@"My Segue" sender:self];
      return;
    } // else do not perform segue
}

I dont think you can stop from at this point. 我不认为你可以在这一点上停下来。

But you can use - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender from iOS 6. 但你可以使用- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender来自iOS 6。

Try giving your segue a different name and only call that segue (eg " [self performSegueWithIdentifier: @"MyRealStartSegue" sender: sender] ") if the alert is not displaying. 尝试给你的segue一个不同的名字,只调用那个segue(例如“ [self performSegueWithIdentifier: @"MyRealStartSegue" sender: sender] ”)如果警报没有显示。

Of course this means you need to check these conditions outside of the " prepareForSegue " method. 当然,这意味着您需要在“ prepareForSegue ”方法之外检查这些条件。 Perhaps an IBAction when some button is pressed? 当按下某个按钮时,也许是IBAction?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM