简体   繁体   中英

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
}

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];

使用self.window.rootViewController.tabBarController可以访问该视图控制器的选项卡栏控制器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM