1

I got a push segue connected in storyboards and then I'm using the prepareForSegue method like this:

- (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. How can I solve this?

nonuma
  • 399
  • 1
  • 3
  • 14

4 Answers4

3

prepareForSegue:sender: is called when the segue is about to be performed. 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:.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • 2
    You can't show the alertview here, because the method will return before the user has selected an option. You need to display and handle the alertview before the segue is called. – Aaron Brager Apr 15 '13 at 12:52
  • Yes, the method will return before the user has selected an option. Why do you think that's a problem? – Jim Apr 15 '13 at 12:55
  • Because @nonuma wants the return value to be determined by the user's selection, which won't be available when the method returns. – Aaron Brager Apr 15 '13 at 12:59
  • @Jim thanks, this works, but if I click it, the alertview shows up but the button stays highlighted, how do I turn this off? – nonuma Apr 15 '13 at 13:13
  • @nonuma, I don't quite follow what you are saying. In any case, that's a separate question, so post it as a new question with more details. – Jim Apr 15 '13 at 13:14
  • @Jim Actually I'm using a Table View Cell, and if you push it, it gets highlighted (default is bleu) but it stays highlighted instead of just highlight for 1 sec – nonuma Apr 15 '13 at 13:16
  • @nonuma, yes that's a separate question. Stack Overflow is not a forum or tech support. This question has been answered. You have a new question, so post a new question. – Jim Apr 15 '13 at 13:17
1

I think you need to reevaluate your logic here and remove the conditional from prepareForSegue

Using 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
}
Rawkode
  • 21,990
  • 5
  • 38
  • 45
0

I dont think you can stop from at this point.

But you can use - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender from iOS 6.

Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • @AaronBrager you can check your condition to return false here. You can call a method to show alert as well. – Vignesh Apr 15 '13 at 12:53
  • @AaronBrager quick search led to this(http://stackoverflow.com/questions/9407571/to-stop-segue-and-show-alert) detailed post . – Vignesh Apr 15 '13 at 12:54
  • @AaronBrager, You are correct if segue has to be decided upon user selection.But I don't think he needs it. – Vignesh Apr 15 '13 at 13:01
0

Try giving your segue a different name and only call that segue (e.g. "[self performSegueWithIdentifier: @"MyRealStartSegue" sender: sender]") if the alert is not displaying.

Of course this means you need to check these conditions outside of the "prepareForSegue" method. Perhaps an IBAction when some button is pressed?

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215