iosobjective-crootview

Setting TabBarViewController as Root View in AppDelegate on launch


This question might seem a bit silly but I've been searching for some time now for an answer but I couldn't find one. So I'm developing an app that integrates Parse push notifications between clients. The app has a UITabBarController (TabBarViewController as the custom class) in the storyboard and at some point in the app (upon receiving a push while the app is closed) I have to set the TabBarViewController as my RootViewController for further action such as [TabBarController doSomething];

In summary, I am trying to display the 3rd tab (index:2) of my TabBarViewController and executing a function found in the TabBarViewController class to display stuff in the view of the 3rd tab.

I am trying to do the following:

In my AppDelegate.m in the didFinishLaunchingWithOptions: method:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
TabBarViewController* tabBarController = (TabBarViewController*)[storyboard instantiateViewControllerWithIdentifier:@"<StoryboardIDofTabBarViewController>"];
tabBarController.selectedIndex = 2;

self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];

[tabBarController doSomething];    

I have a feeling the solution requires delegation but I don't really know how to implement it. My controllers are not embedded in navigation controllers so I'm not sure if I can push the TabBarController.

Any help would be appreciated. Thank you


Solution

  • This works perfect for me in application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    AppDelegate.h

    @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
    

    AppDelegate.m

    UIStoryboard *storyboard = [[UIStoryboard alloc] init];
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    
    UIViewController *tabBarControllerView = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"]; 
    [self.window setRootViewController:tabBarControllerView];
    tabBarController = (UITabBarController *)self.window.rootViewController;
    tabBarController.delegate = (id)self;
    tabBarController.selectedIndex = 2;
    

    ** Verify that the "instantiateViewControllerWithIdentifier" matches the storyBoard ID in the storyboard. Hope this helps.

    Screenshot of storyboard: enter image description here