How to cancel a session associated with UIButton

I am working on this tutorial , which shows how to create an application with drop-down versions. My viewController has two UIButtons, and I connected segue to these UIButtons. These sessions push the new viewController.

Now I am looking for a way to cancel this transition 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 undo the click of a new viewController?

+6
source share
2 answers

Nope. You cannot undo segues that are directly related to interface elements such as your UIButton.

But there is a workaround.

  • Delete the segment associated with the button
  • Add a segue from the viewController (the one that has the button) to the viewController you want to click. It must be a segment that is not associated with an interface element. To do this, you can run segue from the status bar. Or create a segue in the left sidebar.
  • Select this segment, open the attribute inspector and change the segment identifier (e.g. PushRedViewController)
  • Select the Xcodes helper view so that you can see the .h file with viewController using the button.
  • Connect the button to the action. To do this, select the button and drag the control into the .h file. Select an action from the menu and name your action (e.g. redButtonPressed :)
  • Open the implementation of your viewController
  • change the action from step 5 to something like this:

     - (IBAction)redButtonPressed:(id)sender { [self performSegueWithIdentifier:@"PushRedViewController" sender:sender]; } 
+27
source

You can do this by adding the following code to your code

 - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { if([identifier isEqualToString:@"btnSegue"]) { return YES; } else{ return NO; } } 

Let's assume your segue is btnSegue. And if you need to execute segue based on some condition, you can have the following code

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

Where's the BOOL check that you can set to true or false based on your condition.

+9
source

Source: https://habr.com/ru/post/911486/


All Articles