简体   繁体   English

如何在iOS 5中访问标签栏控制器?

[英]How to access tab bar controller in iOS 5?

In Xcode 4.2, I created a new project from the "tabbed application" template. 在Xcode 4.2中,我从“选项卡式应用程序”模板创建了一个新项目。 Now I want to access the tab bar controller in the AppDelegate, but this property is missing. 现在,我想访问AppDelegate中的选项卡栏控制器,但是缺少此属性。 How to solve the problem. 如何解决问题。

In previous version of Xcode, I can access it by calling the self.tabBarController, however, I could not do that in iOS 5. 在以前版本的Xcode中,我可以通过调用self.tabBarController来访问它,但是,在iOS 5中无法做到这一点。

There's no magic happening with the tabBarController property that appears in "tabbed application" template projects. 在“选项卡式应用程序”模板项目中显示的tabBarController属性不会发生任何魔术。 I guess pre-iOS 5 the template was designed so that the tab controller was already set up as an attached outlet for you. 我想iOS 5之前的模板是经过设计的,因此选项卡控制器已经设置为您的附加插座。 If it's not in the template you're using then you just need to add the property yourself. 如果它不在您使用的模板中,那么您只需要自己添加属性。 Below are instructions on how to do this. 以下是有关如何执行此操作的说明。

You just need to add a property for it and then grab the rootViewController from your UIWindow in applicationDidFinishLaunching . 你只需要添加一个属性,然后抓住rootViewController从你UIWindowapplicationDidFinishLaunching ie you want something like this in your app delegate header: 也就是说,您想要在应用程序委托标头中添加以下内容:

@interface MyAppDelegate : NSObject <UIApplicationDelegate>
...
@property (nonatomic, strong) UITabBarController *tabBarController;
...
@end

Then synthesise it in your app delegate implementation and grab it from rootViewController : 然后将其综合到您的应用程序委托实现中,并从rootViewController抓取它:

@implementation MyAppDelegate

@synthesize tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.tabBarController = (UITabBarController*)self.window.rootViewController;
    return YES;
}

@end

if u're using 4.2, i'm pretty sure that on the appDelegate, its automatically added 如果您使用的是4.2,我很确定在appDelegate上,它会自动添加

@property (strong, nonatomic) UITabBarController *tabBarController;

u can access it on the implementation file by using the method u've been using, self.tabBarController 您可以使用自己一直在使用的方法self.tabBarController访问实现文件

just make sure that it's properly synthesized on the implementation file and u write the code in the proper method body 只要确保它在实现文件中正确合成,然后在适当的方法主体中编写代码即可

im sorry but im not very clear of what your problem is 对不起,但我不太清楚你的问题是什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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