2014-03-04 65 views
0
[items addObject:[[UITabBarItem alloc] 
         initWithTitle:@" Add Contacts" image:[UIImage imageNamed:@"images.jpeg"] selectedImage:[UIImage imageNamed : @"checkBox.jpeg"]]]; 

[items addObject:[[UITabBarItem 
         alloc]initWithTitle:@"Contacts" image:[UIImage imageNamed:@"images.jpeg"] selectedImage:[UIImage imageNamed : @"checkBox.jpeg"]]]; 

回答

0

您不能直接將選擇器添加到UITabBarItem

您可以通過- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item委託方法實現該功能。

添加tagsUITabBarItem和實施類似的委託方法:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    if(item.tag == yourTag) 
    { 
     // so your stuff here 
    } 
} 
0

我們需要實現UITabBarControllerDelegate方法如果我們使用的UITabBarController

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; 
0

在故事板中設置uitabbarviewcontroller並定義特定的類。 檢查器字段uitabbarviewcontroller中的每個tabbar設置索引。

然後初始視圖控制器在viewdidload方法中寫下這段代碼。

我認爲這個代碼很有幫助。

UITabBar *tabBar = self.tabBarController.tabBar; 
UITabBarItem *item0 = [tabBar.items objectAtIndex:0]; 

[item0 setTitle:@"Home"]; 
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"home_btn_hred.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home_btn.png"]]; 
[item0 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
           [UIColor darkGrayColor], UITextAttributeTextColor, 
           nil] forState:UIControlStateNormal]; 
[item0 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
           [UIColor redColor], UITextAttributeTextColor, 
           nil] forState:UIControlStateSelected]; 
相關問題