Isuru
Isuru

Reputation: 31323

Fire a segue conditionally to multiple View Controllers

After struggling for days on firing a segue conditionally, I managed to solve it thanks to Simon's answer here. Please take a moment to have a look or you might not understand what I'm talking about below. I didn't copy paste his answer because he's already explained it nicely over there.

Now I've faced a new question. What if I have multiple View Controllers that I want to segue to from one View Controller?

To explain it further : Say I have one MainViewController with 2 buttons. When clicked upon each button, it should segue to their respective View Controller. First button to FirstViewController and the second button to SecondViewController.

The method described in Simon's answer can be used when you segue from one View Controller to another View Controller. Since in that method, you tie the segue to the View Controller itsrlf and not to the button, you have only one segue with an identifier for that particular View Controller. Therefore I cannot distinguish between the button taps separately.

Is there a workaround to solve this problem?

Thank you.

Upvotes: 4

Views: 7088

Answers (3)

Code by Larry
Code by Larry

Reputation: 1

I just wrote another way to call multiple detail views from a single table. Each cell could essentially make a different view be displayed. The code is similar to what you see in this post but you essentially use identifiers and attributes on the list item to determine which view to show.

https://codebylarry.com/2016/07/15/multiple-detail-views-in-swift/

override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == 1 {
        self.performSegueWithIdentifier("secondView", sender: self)
    } else {
        self.performSegueWithIdentifier(“others", sender: self)
    }
}

Upvotes: 0

Khaled Barazi
Khaled Barazi

Reputation: 8741

I suggest you look a bit at reworking your code logic.

If I understand correctly, you have a VC (embedded in a Nav. Controller) with 2 buttons and you have figured out how to segue each button to a different VC.

Your problem is you want to make sure that even if one of the buttons are pressed, a validation is done before an action takes place. I would advise this is bad User Interface design because the user has the illusion that this button might do something and then they click it and nothing happens.

UIButton can be connected to IBActions (to initiate actions) and IBOutlets (to set their properties). If this is a button created in IB directly, I would connect it to your class as an Outlet property:

@property (nonatomic,weak) IBOutlet UIButton* myButton;

And then set its enabled value:

self.myButton.enabled=NO;

This will keep the button and dim it. This is much better UI design and the user knows they should not press the button because some condition is not satisfied.

I would rework the code so that you set this value as disabled by default for example and enable it appropriately in your code whenever your "condition" is satisfied.

Obviously if this button is created programmatically (in your code without IB) then it is easy to just use the second command above.

Hope this helps.

Upvotes: 0

Kishor Kundan
Kishor Kundan

Reputation: 3165

It might be bit premature to say this but I guess you should look into Segue more deeply.

Yes you can perform segure from button. Just control click the button and drag the cursor to view controller you want it SEGUE'd. And from my understanding only condition there is each button tap results a segue to a fixed view. There is no condition there.

Also, you can push the navigation controller manually by

YourViewController *destViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"YourDestinationViewId"];
[self.navigationController pushViewController:destViewController animated:YES];

UPDATE:

prepareForSegue is too late to stop a segue from proceeding. Yes you can create multiple segues from your view to other view controllers. And in this case you have to do so. Don't reate a segue from button, just define a IBACtion on the button click you can do the validation from there,

if(validationSuccess) {
     [self performSegueWithIdentifier:@"segue1" sender:self];
} 

if you are using ios6

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

return YES on validation success and NO on failure to stop it from proceeding.

Upvotes: 4

Related Questions