开发者

How to create Tabbar with default items Dynamically?

I have application in which i have create tab-bar dynamically. Now i want to add default items like contact, more, about, favorite etc. How i add all these item dynamically with Tab-Bar?

CGRect myTab =CGRectMake(0,368,320,49);
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:myTab];  

[self.view addSubview:tabBa开发者_C百科r];


Usually you would be creating a TabBar using a UITabBarController, in which case you can simply set the property

@property(nonatomic, copy) NSArray *viewControllers

If you're sure you wish to create a UITabBar then you want to use the items property. Something like this:

- (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...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜