7

I'm working on this tutorial, which shows how to create an app with storyboards. My viewController has two UIButtons and I have connected a segue to those UIButtons. These segues push a new viewController.

Now I am looking for a way to cancel this segue if a certain condition becomes true. When I used the old xib files to create my interface I was able to do something like this:

-(IBAction)sendButton:(id)sender {
  if(TRUE) {
    // STOP !!  
  }
}

This does not work anymore. How can I cancel the push of the new viewController?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Qooe
  • 673
  • 2
  • 7
  • 13
  • Your question is unclear. How are your elements hooked up? Where is the code that makes the navigation "go to a new view"? – Stavash Mar 23 '12 at 09:58
  • The user can manually cancel a click response by dragging their finger off of the button before releasing it... Is this what you mean? I agree with the above comment, this question is unclear. – Mick MacCallum Mar 23 '12 at 10:11
  • You have to read the tutorial, hidden behind the link, to understand the question. It's about a storyboard segue linked to a UIButton – Matthias Bauch Mar 23 '12 at 10:24
  • @Qooe I rewrote the whole question so it makes more sense. I hope I did understood correctly what you wanted to ask. If that's not the case feel free to rollback to your version in the [revisions](http://stackoverflow.com/posts/9837102/revisions) – Matthias Bauch Mar 23 '12 at 10:32
  • @Matthias Bauch, thanks. It's more clear. – Qooe Mar 23 '12 at 10:42

2 Answers2

27

Nope. You can't cancel segues that are directly linked to interface elements like your UIButton.

But there is a workaround.

  1. Remove the segue that is linked to the button
  2. Add a segue from the viewController (the one with the button) to the viewController that should be pushed. This must be a segue that is not connected to a interface element. To do this you can start the segue from the status bar. Or create the segue in the left sidebar.
  3. Select this segue, open the attributes inspector and change the Identifier of the segue (e.g. PushRedViewController)
  4. Select Xcodes assistant view, so you can see the .h file of the viewController with the button.
  5. Connect the button to an action. To do this select the button and control-drag to the .h file. Select action in the menu and name your action (e.g. redButtonPressed:)
  6. Open the implementation of your viewController
  7. change the action from step 5 to something like this:

    - (IBAction)redButtonPressed:(id)sender {
        [self performSegueWithIdentifier:@"PushRedViewController" sender:sender];
    }
    
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
11

You can actually do this by adding following method into your code

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

if([identifier isEqualToString:@"btnSegue"])
{

   return YES;
}
else{
    return NO;
}

}

Lets assume your segue is btnSegue.And if you need the segue to be performed based on some condition you can have following code

if(check)
{
 [self performSegueWithIdentifier:@"btnSegue" sender:self];
}

Where check is BOOL which you can set true or false based on your condition.

Aadil Keshwani
  • 1,385
  • 1
  • 18
  • 29