2011-07-25 32 views
0

我有應用程序,我已經動態地創建標籤欄。現在我想添加默認項目,如聯繫人,更多,關於,最喜歡等。我如何使用Tab-Bar動態添加所有這些項目?如何使用默認項目動態創建Tabbar?

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

[self.view addSubview:tabBar]; 

回答

3

通常你會創建使用的UITabBarController一個TabBar,所以在這種情況下,你可以簡單地設置屬性

@property(nonatomic, copy) NSArray *viewControllers 

如果你確定你想創建中的UITabBar一樣,那麼你要使用項目屬性。類似這樣的:

- (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

我如何添加默認項目聯繫人? – ram

+0

我如何設定物品的目標? – ram

+0

我編輯了我的回覆以解決您的問題。 – Johnus

相關問題