[英]Adding UITabBarItem to a UITabBar in a UIViewController
So I have a UIViewController
that has a UITabBar
in the view. 所以我有一个UIViewController
在视图中有一个UITabBar
。 I want to pull down JSON and decided what tabs I want to add to the tab bar, so I need to be able to choose the tabs on runtime. 我想下拉JSON并确定我想要添加到选项卡栏的选项卡,所以我需要能够在运行时选择选项卡。 I wanted to use the UITabBarController setViewControllers:animated:
method to add to it a list of view controllers. 我想使用UITabBarController setViewControllers:animated:
方法向其中添加一个视图控制器列表。 However since I am doing this in a view controller I do not know how to gain access to the tab bar's view controller. 但是,由于我在视图控制器中执行此操作,因此我不知道如何访问选项卡栏的视图控制器。 Here is my code ... 这是我的代码......
Header 头
#import <UIKit/UIKit.h>
@interface HealthTicketTabController : UIViewController <UITabBarDelegate, UITabBarControllerDelegate> {
NSArray *viewControllers;
IBOutlet UITabBar *tabBar;
UIViewController *selectedViewController;
// How do I link this the the tabBar's view controller???
UITabBarController *tabBarController;
}
@property (nonatomic, retain) NSArray *viewControllers;
@property (nonatomic, retain) IBOutlet UITabBar *tabBar;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) UIViewController *selectedViewController;
@end
Source 资源
- (id)init
{
self = [super initWithNibName:@"HealthTicketTabView" bundle:nil];
if (self)
{
//Controllers for the tab view
HealthCardController *card = [[HealthCardController alloc] init];
MedicalExpensesController *medical = [[MedicalExpensesController alloc] init];
//Tab bar items to be displayed in the tab bar
card.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0];
medical.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1];
NSArray *array = [[NSArray alloc] initWithObjects:card, medical, nil];
//Set the tab bar's view controllers to the list
tabBarController.viewControllers = [NSArray arrayWithObjects:card, medical, nil];
[array release];
[card release];
[medical release];
}
return self;
}
UIViewController has a tabBarController property already. UIViewController已经有一个tabBarController属性。 You do not need one in your header file. 您的头文件中不需要一个。 This is how you can access the UITabBarController. 这是您可以访问UITabBarController的方法。
Found out that since I was using a UIViewController
to control the UITabBar
I need to set the tab bar's delegate to the current UIViewController
and handle the tab events in the current controller. 发现由于我使用UIViewController
来控制UITabBar
我需要将标签栏的委托设置为当前的UIViewController
并处理当前控制器中的tab事件。
Adding TabBarItems to the TabBar 将TabBarItems添加到TabBar
[self.tabBar setItems: tabBarItems animated:TRUE];
Switching between ViewControllers 在ViewControllers之间切换
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
UIViewController *temp = [viewControllers objectAtIndex:item.tag];
[self.selectedViewController.view removeFromSuperview];
[self.view addSubview:temp.view];
self.selectedViewController = temp;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.