6

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.

4 Answers 4

27

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]; 
Sign up to request clarification or add additional context in comments.

9 Comments

Ok thanks for that. And if i want to get the first view controller of my navigation controller ? myStoryBoardInitialViewController.topViewController ?
topViewController will give you the topMost viewController on the navigation stack..try this to fetch first viewcontroller.. [myStoryBoardInitialViewController.viewControllers objectAtIndex:0];
When i am doing so i got the following error-message : Warning: Attempt to present <UINavigationController: 0x9f6fee0> on <UINavigationController: 0xab40560> whose view is not in the window hierarchy! :/
Code : UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Storyboard~IPhone" bundle:[NSBundle mainBundle]]; UINavigationController * myStoryboardInitialViewController = [storyboard instantiateInitialViewController]; [[myStoryboardInitialViewController.viewControllers objectAtIndex:0]performSegueWithIdentifier:@"showLoginScreen" sender:self];
I want to ask something that instantiateInitialViewController will be instantiating the initial view controller again or it will be returning the current instance which already exists?
|
4

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!

5 Comments

I am trying to implement the Facebook Connect. And if my app launches and a session isnt still available i want to show the login view which is connected to my initialview controller via a seque... maybe this screenshot helps you to understand my problem : imageshack.us/photo/my-images/820/bildschirmfoto20121108u.png
Sebastian, seems like you want to display not the initial view controller, but the one connected via segue to the first. (Reading your initial question again, I could have guessed that...) In addition to the instantiateInitialViewController method, there is a instantiateViewControllerWithIdentifier method on the storyboard object. So you would have to name your second scene, and use that name to get access to it.
Hmm , it still doesnt work. I used the following code UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Storyboard~IPhone" bundle:[NSBundle mainBundle]]; UINavigationController * myStoryboardInitialViewController = [storyboard instantiateViewControllerWithIdentifier:@"MenuViewController"]; [myStoryboardInitialViewController performSegueWithIdentifier:@"showLoginScreen" sender:self];.. inside the console i get this message Attempt to present <UINavigationController: 0x9faac20> on <MenuViewController: 0x9fa9b50> whose view is not in the window hierarchy!
Sebastian, I don't know how you named the view controllers (their "identifiers"). Is "MenuViewController" the first or the second row? If it's the first, you can get it more easily with instantiateInitialViewController. If it's the second, as I suspect, you don't need to call performSegueWithIdentifier, just present it. Crossing a segue means to go from one scene to another, and as you don't have one on the screen yet, performSegueWithIdentifier is not what you want. Anyway, the segue leads from the first to the second, so I was expecting a different error in this case... hmm.
... (continued) I guess you just need to say [vc presentViewController] instead of the performSegue method. I'm not sure, I never did that manually. Grüße von nobi
0

In my app i have tabBar

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; TabBarController *tabBar = [storyBoard instantiateViewControllerWithIdentifier:@"TabBarController"]; _window.rootViewController = tabBar; 

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.