xcode 标签栏控制器(故事板模板)和 AppDelegate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11709481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tab Bar Controller (storyboard template) and AppDelegate
提问by ingenspor
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.
当我为带有故事板的 TabBarController 创建 XCode 4 iPhone 模板时,它会自动配置一个主视图控制器和所有内容。但是 AppDelegate 中的 Tab Bar Controller 没有属性。我可以为它创建一个插座,并尝试将它与故事板中的 Tab Bar Controller 链接,但这是不可能的。有没有更好的方法来访问 didFinishLaunchingWithOptions 方法中的 Tab Bar Controller,因为它已经连接上了?我想要的是 self.currentController = 标签栏控制器中的当前标签。
AppDelegate.h:
AppDelegate.h:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m:
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
}
回答by Phillip Mills
Assuming things are set up in your storyboard as expected, this should give you a reference to the tab bar controller in didFinishLaunchingWithOptions:
:
假设您的故事板中的内容按预期设置,这应该为您提供对标签栏控制器的参考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];
回答by The Kraken
Use self.window.rootViewController.tabBarController
to gain access to that view controller's tab bar controller.
用于self.window.rootViewController.tabBarController
访问该视图控制器的标签栏控制器。