Conditional Segue Using Storyboard
I just need to know how to make some 'conditional' redirect / segue to some view. For example :
If user haven't login yet, then a view with login form appears. But if user logged in already, they will see their profile view.
How to make conditional segue like that using storyboard?
Thank you
For conditional segues, don't start a segue from a button as you normally do. Instead of that use the following steps:
In your storyboad, start a segue directly from the source view controller to the destination view controller. For doing this, you can drag your source view controller's icon on the bar just below the view to the destination view controller on the main canvas area. Just remember you have to connect two view controllers directly and not with any control.
Enter a suitable segue identifier for this segue. For example say "conditionSegue"
-
Now, in your source view controller’s .m file perform your condition and call
-performSegueWithIdentifier:sender:
method on "self" like this:-(void)loadDestinationVC{ if(condition == YES){ [self performSegueWithIdentifier:@"conditionSegue" sender:nil]; } }
I hope I made it clear.
I believe a better approach if you want to use segues from buttons would be to use this:
First create a button and use a segue to connect it to another view, give this segue a name
then use this method in your .m file
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:@"SegueName"]) {
//Check if should make the segue
//if not do something
[self doSomething];
//and return NO;
return NO;
}
return YES;
}
In Swift you could do : as @Gaurav Singh suggested in his answer
Step 1: Insert a segue from ViewControllerA to ViewControllerB. The segue should start from ViewControllerA itself and not from any control ( like Button ) in it.
Step 2: Give segue an unique identifier in storyboard. Ex: seageFromAtoB
Step 3 : In your ViewControllerforA.swift write :
if(condition == true) {
self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}
If you are performing some task in some other Thread
then using performSegueWithIdentifier
can throw an exception.
If you get this error then you should use :
if(condition==true){
NSOperationQueue.mainQueue().addOperationWithBlock {
self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}
}
This will perform segue redirection in main Thread
.
Hope this help for someone looking for Swift option
You make the segues directly from one controller to another (or others), and give them identifiers. In code you call choose between segues depending on your conditions, and call performSegueWithIdentifier:sender:.
Create "generic" segue by ctrl dragging from the view controller icon at the bottom to the destination. This segue won't be associated with any action. Then, in your code where ever the event is using your conditional code that you want to trigger the segue, call:
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender