When is shouldPerformSegueWithIdentifier called?
When I run it shouldPerformSegueWithIdentifier is called first and second the IBAction
Only one of them should be called. The fact that they are both called means that your storyboard configuration is incorrect (in both cases).
Using shouldPerformSegueWithIdentifier( ) method in Swift
You may not invoke shouldPerformSegueWithIdentifier()
method by yourself. It will be automatically called just before transition to the next view giving a chance to determine wether the transition should take place or. You may conditionally return YES/NO from this method. If your condition does't involve any sever call,a simple logical checking this method will be enough for you.
performSegueWithIdentifier()
is used to invoke a segue programmatically. Consider the above case with a network call, you may return NO from shouldPerformSegueWithIdentifier()
initially since authentication is going on. After getting the response from server if it success you can call the segue to execute with performSegueWithIdentifier
(Here the identifier is the ID you have given in the storyboard). Before make sure you are supposed to return YES from shouldPerformSegueWithIdentifier()
.
Now a third case if your segue is connecting from the login button(You have to connect it from the controller itself). The checking of shouldPerformSegueWithIdentifier
is no more required. You can just call the segue with performSegueWithIdentifier()
after getting the success response from server
Perform a segue only if something happens
This answer explains what's happening with your logic.
What you can do instead, check if the textfield is filled inside the button function before performing the segue.
Prevent segue in prepareForSegue method?
It's possible in iOS 6 and later:
You have to implement the method
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
In your view controller. You do your validation there, and if it's OK then return YES;
if it's not then return NO;
and the prepareForSegue is not called.
Note that this method doesn't get called automatically when triggering segues programmatically. If you need to perform the check, then you have to call shouldPerformSegueWithIdentifier to determine whether to perform segue.
performSegueWithIdentifier not firing
Ok I solved my issue. It had to do with saving the currently displayed view controller when the app goes to background and then perform the segue before the app goes active. And thanks for the downvote. Really makes me feel at home!
To stop segue and show alert
If your deployment target is iOS 6.0 or later
You can simply implement the shouldPerformSegueWithIdentifier:sender:
method on your source view controller. Make this method return YES
if you want to perform the segue, or NO
if you don't.
If your deployment target is earlier than iOS 6.0
You will need to change the way your segue is connected in the storyboard and write a little more code.
First, set up the segue from the button's view controller to the destination view controller, instead of directly from the button to the destination. Give the segue an identifier like ValidationSucceeded
.
Then, connect the button to an action on its view controller. In the action, perform the validation and either perform the segue or show an alert based on whether the validation succeeded. It will look something like this:
- (IBAction)performSegueIfValid:(id)sender {
if ([self validationIsSuccessful]) {
[self performSegueWithIdentifier:@"ValidationSucceeded" sender:self];
} else {
[self showAlertForValidationFailure];
}
}
performSegue throwing SIGABRT
sigabrt error calling segue programatically in swift
Make sure you don't have any orphaned connections from your view controller to your storyboard.
Related Topics
iOS Controlling Uiview Alpha Behaviour for Subviews
Can't Load Uiviewcontroller Xib File in Storyboard in Swift
Codesign Returned Unknown Error -1=Ffffffffffffffff
Asset Catalog: Access Images with Same Name in Different Folders
Does Webkit in iOS 11 (Beta) Support Webrtc
How to Create a Uiimage from the Current Graphics Context
iOS 11 Layout Guidance About Safe Area for iPhone X
Posting Photos to Facebook Fan Page with iOS Sdk
Selecting a Word in a Uitextview
Uicontroleventtouchdragexit Triggers When 100 Pixels Away from Uibutton
Setkeepalivetimeout iOS Behavior, Exceeded 15 Wakes in 300 Sec
Open Links in Safari Instead of Uiwebview
Does Webkit in iOS 11 (Beta) Support Webrtc
How to Do Weak Linking in Swift
Locationmanager:Didenterregion Not Called When a Beacon Is Detected
How to Deserialize an Escaped JSON String with Nsjsonserialization