【问题标题】:How to create Tabbar with default items Dynamically?如何动态创建带有默认项目的标签栏?
【发布时间】:2011-07-25 04:31:59
【问题描述】:
我有动态创建标签栏的应用程序。现在我想添加默认项目,如联系人、更多、关于、收藏等。我如何使用 Tab-Bar 动态添加所有这些项目?
CGRect myTab =CGRectMake(0,368,320,49);
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:myTab];
[self.view addSubview:tabBar];
【问题讨论】:
标签:
iphone
xcode
uiview
uinavigationcontroller
uitabbaritem
【解决方案1】:
通常你会使用 UITabBarController 创建一个 TabBar,在这种情况下你可以简单地设置属性
@property(nonatomic, copy) NSArray *viewControllers
如果您确定要创建 UITabBar,那么您要使用 items 属性。像这样的:
- (void) setupTabBar {
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:myTab];
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];
// Add a 'test' item with no image and the text "Test"
[items addObject:[[[UITabBarItem alloc] initWithTitle:@"Test" image:nil tag:1] autorelease] ];
// Add a 'contacts' item
[items addObject:[[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2] autorelease] ];
// Put the items in the tab bar
tabBar.items = items;
// Setup this object to respond to tab changes
tabBar.delegate = self;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if (item.tag == 2) {
// Contacts was selected. Do something...
}
}