UIStoryBoard get first view controller from ApplicationDelegate

16,178

Solution 1

I am not sure if I am understanding the question correctly I think you are asking how do you get the first viewController in a storyboard. To get this you call,

UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc =[storybord instantiateInitialViewController];

Change the name of the storyboard to suit your name, MainStoryboard is just the default name. Hope this is what you were looking for.

Edit:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc =[storybord instantiateInitialViewController];
    //set the delegate on the view controller that you have loaded
    // Override point for customization after application launch.
    return YES;
}

Solution 2

Rather then creating new copy of your existing storyboard using

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

you can set in your app delegate's header file this property

@property (nonatomic, weak) UIViewController* initialViewController;

and in this method simply assign self.window.rootViewController to the property

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.initialViewController = self.window.rootViewController;

    return YES;
}

This should work, because in window property of app delegate protocol is access to rootViewConroller and this controller is initialViewController if using storyboards.

Share:
16,178
Aran Mulholland
Author by

Aran Mulholland

Full stack, Javascript, HTML, .NET, .NET Core, ASP.NET MVC, Project Coding Infrastructure, iOS, neo4j development, analogue synthesis, toilet photographer.

Updated on June 23, 2022

Comments

  • Aran Mulholland
    Aran Mulholland almost 2 years

    I have a protocol that my ApplicationDelegate implements. I want to pass this to the first view controller defined in a story board. How do I get access to it from the method?

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    
  • Aran Mulholland
    Aran Mulholland over 12 years
    yep that's what I needed. any idea about how to load the correct storyboard for the device in a universal app (iphone vs ipad)?
  • Scott Sherwood
    Scott Sherwood over 12 years
    This should do it UIViewController *vc; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainiPadStoryboard" bundle:nil]; vc = [storybord instantiateInitialViewController]; } else{ UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainiPhoneStoryboard" bundle:nil]; vc = [storybord instantiateInitialViewController]; }
  • Bob Spryn
    Bob Spryn about 12 years
    Pretty sure this is grabbing a new copy, not the existing one.
  • appmattus
    appmattus over 11 years
    If you don't want to hard code the storyboard name, which as Scott points out will be different for iPhone and iPad the name is defined in the info.plist file, it can be extracted as follows: NSString *storyBoardName = [[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"]; self.storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:[NSBundle mainBundle]];
  • hotdogsoup.nl
    hotdogsoup.nl over 8 years
    This works, and is the correct answer; instantiating a new controller is not the same as pointing to the already instantiated one from the storyboard.
  • Martin
    Martin about 8 years
    try to understand the problem instead of just converting the accepted answer into swift...
  • Chamath Jeevan
    Chamath Jeevan about 8 years
    @Martin I have edited the answer. Hope even a IOS beginner can understand now.
  • Martin
    Martin about 8 years
    but your solutions creates a new view controller while the @aran was searching for a solution to get the existing first view controller. The currently accepted answer is not a good answer.
  • Chamath Jeevan
    Chamath Jeevan about 8 years
    @Martin The currently accepted answer is NOT my answer. Its By Scott Sherwood. If the correct answer is not a good one why don't you add the correct answer than commenting on others answer. Also how you deside anwers for one else problum ? Only the issue ower (Aran Mulholland)can mark it as correct answer. Aran Mulholland marked the answer as correct becasue that solution works for him.
  • Martin
    Martin about 8 years
    because the correct answer is already here (see @PeterStajger one) and was poster 2 years after the question was asked. Maybe the asker did accept the accepted one because there was no better answer at the time. AranMulholland wants, in his original question, to get the first view controller existing instance. Giving a newly created one in this context wouldn't make any sense.