Sebastian Boldt
Sebastian Boldt

Reputation: 5321

Getting Initial View Controller from Storyboard inside the App Delegate

I am trying to access the initial viewcontroller of my storyboard. It is a navigation controller which is wired up to a second viewcontroller via a seque. So after my application did finish launching I want to directly show the viewController that is connected through the segue. I tried it out with code like this but it doesn't work ...

UINavigationController *navController = (UINavigationController*)self.window.rootViewController;
[navController.topViewController performSegueWithIdentifier:@"showLoginScreen" sender:self];

What am I doing wrong?

4 years later:

If I look at my question again after 4 years, I honestly have no idea what my real problem was.

Upvotes: 6

Views: 20877

Answers (4)

Tom Condon
Tom Condon

Reputation: 139

Ok, here is a reason in the year 2022 pre SwiftUI apps. Your apps starts... it has several choices, one, it wants you to login, two it wants you to sync with home base, and three everything is fine and just show the main UI. Case two may refresh a rather large body of information before you build out you main UI.

Upvotes: 0

AppleDelegate
AppleDelegate

Reputation: 4249

Be sure you have tick marked the

is initial ViewController

option for UINavigationController in storyBoard

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UINavigationController * myStoryBoardInitialViewController = [storyboard instantiateInitialViewController];

Upvotes: 27

Pitono
Pitono

Reputation: 156

In my app i have tabBar

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

TabBarController *tabBar = [storyBoard instantiateViewControllerWithIdentifier:@"TabBarController"];

_window.rootViewController = tabBar;

Upvotes: 0

virtualnobi
virtualnobi

Reputation: 1180

Sebastian, I'm not sure why you would want to start the initial view controller manually - all my projects (which all use storyboards) do this automatically if you ticked "Use Storyboards" on the second screen in the "New Project" wizard.

Maybe you need to mark the storyboard scene as inital? This can be done in the scene's attribute inspector by ticking the "Is Initial View Controller" - rather obviously named.

And then - if you really have a unusual setup which requires you to access the scenes manually, you can use the following:

UIStoryboard *sb = [UIStoryboard storyboardWithName: "StoryboardName"
                                             bundle: [NSBundle mainBundle]];
UIViewController *vc = [sb instantiateInitialViewController];

(Beware - no code completion here, so check spelling again.)

Or maybe I am getting your question completely wrong..? Happy hacking!

Upvotes: 4

Related Questions