Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab Bar Controller (storyboard template) and AppDelegate

When I create a XCode 4 iPhone template for TabBarController with storyboard, its automaticly configured with a main view controller and everything. But there is no propery for the Tab Bar Controller in the AppDelegate. Can I created an outlet for it, and tryed to link it with my Tab Bar Controller in storyboard but that's not possible. Is there a better way to access the Tab Bar Controller in didFinishLaunchingWithOptions method, as it's already kind of hooked up? What I want is self.currentController = current tab in Tab Bar Controller.

AppDelegate.h:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m:

@interface AppDelegate()

@property (nonatomic, assign) UIViewController<SubViewContainer> *currentController;

@end

@synthesize window = _window
@synthesize currentController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

//I need this piece of code to equal the Tab Bar Controller current tab
self.currentController = ?

return YES;
}

//And I'm gonna use this void for some statements about the Tab Bar Controller tabs:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:
(UIViewController *)viewController
{
// with some statements
}
like image 730
ingenspor Avatar asked Nov 29 '22 14:11

ingenspor


1 Answers

Assuming things are set up in your storyboard as expected, this should give you a reference to the tab bar controller in didFinishLaunchingWithOptions::

NSLog(@"Root: %@", self.window.rootViewController);
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;

Usually, you could get the current controller using...

self.currentController = [tabController selectedViewController];

...but since no controller has been selected at the time this method executes, the best guess of what you want is...

self.currentController = [[tabController viewControllers] objectAtIndex:0];
like image 91
Phillip Mills Avatar answered Dec 10 '22 02:12

Phillip Mills