2013-09-28 80 views
0

我一直在試圖改變我的選項卡的標題用下面的代碼,更改標題中的UITabBarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) { 

     self.title = @"City Search"; 
     self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1]; 
    } 

    delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    return self; 
} 

這給了我一個iOS原生搜索圖標和測試。但我想要做的就是添加自己的標題,我試着做以下,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) { 

     self.tabBarItem = [[UITabBarItem alloc] init] 
     self.tabBarItem.image = myImage; 
     self.tabBarItem.title = @"FooBar"; 
    } 

    delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    return self; 
} 

然而,這只是給了我一個空白標籤,沒有任何文字,一切工作正常。你能幫忙嗎? 我使用iOS 6

回答

0

實際遵循的工作:)不過我已經注意到,如果圖像文件不存在,這是不行的...

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    if (self) { 

     self.title = @"City Search"; 
     [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"about_tap_icon.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"about_tap_icon.png"]]; 
     [self.tabBarItem setBadgeValue:@"about"]; 
     [self.tabBarItem setTitle:@"hello"]; 
    } 

    return self; 
} 
2

這裏有一個簡單的解決方案, 你的形象必須是黑色和白色,並在適當的大小下面的代碼30X30

self.title=NSLocalizedString(@"foobar", nil); 
    self.tabBarItem.image=[UIImage imageNamed:@"image.png"]; 
+0

嗨抱歉後期靠,這工作,但它也改變了標題欄標題? –

2

使用:

myTabItemContainViewController *addVC = [[myTabItemContainViewController alloc] init]; /// initialize object of your viewController  
self.tabBarItem = [[UITabBarItem alloc] init] 
self.tabBarItem.image = myImage; 
self.tabBarItem.title = @"FooBar"; 
[addVC setTabBarItem: self.tabBarItem ]; 

您需要將你的tabBarItem添加到你特定的viewController。

+0

你好:)對不起,遲交回復。這似乎沒有工作? –

0

這個UIViewController的UITabBarItem已經被創建和添加 - 所以不需要分配一個新的。

只需修改當前的UITabBarItem。

您還可以設置選擇圖像,像這樣:

-(void)awakeFromNib 
{ 
    self.title = @"City Search"; 
    [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"citySearch_selected"]withFinishedUnselectedImage:[UIImage imageNamed:@"citySearch_unselected"]]; 
}