2
votes

I like to segue from the current view controller to the "settings" view controller. Which method is more efficient to transition and why? Thanks! I have to segue in code because I have to observe a condition at run time.

Method 1:

UINavigationController *navigationController = (UINavigationController *)[[[UIApplication sharedApplication] delegate] window].rootViewController;
SettingsViewController *v = [self.storyboard instantiateViewControllerWithIdentifier:@"settings"];
[navigationController pushViewController:v animated:YES];

Method 2: In the storyboard, control-drag the current view controller icon (bottom left) to "settings" view controller and then name the segue identifier "gotoSettingsVC", set style to "push" and then use this code...

[self performSegueWithIdentifier:@"gotoSettingsVC" sender:nil];
1
It depends on what you're trying to accomplish. For most usages, it's going to be far simpler to implement and maintain if you're using performSegueWithIdentifier, or even better yet, just having the control in the storyboard directly trigger the segue. The drawback is that some people aren't comfortable with using prepareForSegue and it can get kind of ugly if you have a lot of exits from a view controller.David Berry
@David, I didn't mention that I have to trigger the segue from code because I have to observe a condition. Edited question to reflect this.Loozie
In that case, just look at your code, 3 long moderately complicated lines, vs. one shorter line. Which is more transparent about what you're trying to accomplish :)David Berry
Oh, just for simplicity, the first line can probably be replaced with self.navigationController unless you're doing something really weird.David Berry
Try SMQuickSegue (github.com/stefanomondino/SMQuickSegue) if you have view controllers with many exits in a storyboard based project.Stefano Mondino

1 Answers

3
votes

Both work just fine.

If you're using storyboards, segues are probably the better way to go. Instantiate the initial view controller and let the segues be your guide.

Also, while Method 1 can be used with storyboards, unlike segues, it also can be used with regular xibs (or no interface builder at all) as well.

Other than that, it comes down to preference. Some people hate the interface builder and others swear by it. ;)