当我使用storyboard为TabBarController创建XCode 4 iPhone模板时,它会自动配置主视图控制器和所有内容。但AppDelegate中的Tab Bar Controller没有任何特色。我可以为它创建一个插座,并尝试将其与故事板中的Tab Bar Controller链接,但这是不可能的。有没有更好的方法来访问didFinishLaunchingWithOptions方法中的标签栏控制器,因为它已经有点连接?我想要的是标签栏控制器中的self.currentController =当前选项卡。
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
}
答案 0 :(得分:19)
假设事情在故事板中按预期设置,这应该为您提供didFinishLaunchingWithOptions:
中标签栏控制器的参考:
NSLog(@"Root: %@", self.window.rootViewController);
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
通常,您可以使用...
获取当前控制器self.currentController = [tabController selectedViewController];
...但是由于此方法执行时没有选择任何控制器,因此最好猜测你想要的是......
self.currentController = [[tabController viewControllers] objectAtIndex:0];
答案 1 :(得分:2)
使用self.window.rootViewController.tabBarController
访问该视图控制器的标签栏控制器。