Alper
Alper

Reputation: 3993

Looking to maximize reuse in an iPhone storyboard application

I'm programming a complex storyboard application and I'm encountering some issues when it comes to code re-use across view controllers and different application paths.

Can I segue to a ViewController that is not directly connected to the current one?

How do I segue out from a button to several ViewControllers conditionally? Connecting both does not work.

Can I enter the ViewController sequence from an arbitrary position in the application?

Just a few questions that come up. Anybody have any ideas or good examples?

Upvotes: 2

Views: 1588

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

Can I segue to a ViewController that is not directly connected to the current one?

Not with a segue. But you can push or present a viewController from a storyboard modally, like you did with .xib files.

// if self was created from code or from a .xib:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
// if self was instantiated from within a storyboard:
UIStoryboard *storyBoard = self.storyboard;  

MyFancyViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"MyFancyViewControllerIdentifier"];
[self presentModalViewController:viewController animated:YES];

But you have to set the identifier first. Select the viewController in the storyboard and do that in the attributes inspector.

Once the MyFancyViewController is visible you can use all its segues. It might feel strange to switch between code and storyboard segues, but there is nothing wrong with that. It makes the whole storyboard thing really usable.


How do I segue out from a button to several ViewControllers conditionally? Connecting both does not work.

Add two or more segues that start from the viewController (not from a button or any other view. E.g. start your control-drag at the statusbar) to the target viewControllers. Set identifiers for them and use some code like this:

if (someState) {
    [self performSegueWithIdentifier:@"MyFirstSegueIdentifier" sender:somethingOrNil];
}
else {
    [self performSegueWithIdentifier:@"MySecondSegueIdentifier" sender:somethingOrNil];
}

Can I enter the ViewController sequence from an arbitrary position in the application?

Yes, if you use "old school" view controller management. Similar to the first part. Instantiate your viewController and present it with code.

Upvotes: 6

Related Questions