UIStoryBoard get first view controller from ApplicationDelegate
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.
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, 2022Comments
-
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 over 12 yearsyep 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 over 12 yearsThis 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 about 12 yearsPretty sure this is grabbing a new copy, not the existing one.
-
appmattus over 11 yearsIf 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 over 8 yearsThis 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 about 8 yearstry to understand the problem instead of just converting the accepted answer into swift...
-
Chamath Jeevan about 8 years@Martin I have edited the answer. Hope even a IOS beginner can understand now.
-
Martin about 8 yearsbut 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 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 about 8 yearsbecause 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.