2012-03-02 81 views
3

我試圖在我的UINavigationController的工具欄中添加自定義標籤。我跟着這個question的頂部答案,但它似乎並沒有爲我工作,我不知道爲什麼。自定義文本不會顯示,但按鈕在那裏。它突出顯示了我按下它但沒有文字。在工具欄中添加自定義標籤不起作用

這裏是我的代碼:

- (void) editToolbar{ 
    NSArray *toolbarItemsCopy = [self.toolbarItems copy]; 

    //set up the label 
    self.notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width/0.25, 21.0f)]; 
    self.notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:20]; 
    self.notificationsLabel.backgroundColor = [UIColor clearColor]; 
    //self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0]; 
    self.notificationsLabel.textColor = [UIColor blueColor]; 
    self.notificationsLabel.text = @"Checking server"; 
    self.notificationsLabel.textAlignment = UITextAlignmentCenter; 

    //init a button with custom view using the label 
    UIBarButtonItem *notif = [[UIBarButtonItem alloc] initWithCustomView:self.notificationsLabel]; 

    //add to the toolbarItems 
    NSMutableArray *toolbarItemsMutableArray = [[NSMutableArray alloc]init]; 

    NSLog(@"toolbar %i", self.toolbarItems.count); 

    //have to add the custom bar button item in a certain place in the toolbar, it should be the 3rd item 
    for (int i = 0; i < toolbarItemsCopy.count; i++) { 
     if (i == 2){     
      [toolbarItemsMutableArray addObject:notif]; 
     } 
     NSLog(@"toolbar item %i %@", i,[toolbarItemsCopy objectAtIndex:i]); 
     [toolbarItemsMutableArray addObject:[toolbarItemsCopy objectAtIndex:i]]; 
    } 
    if (toolbarItemsCopy.count == 4){ 
    }else{ 
     //remove the previous custom label 
     [toolbarItemsMutableArray removeObjectAtIndex:3]; 
    } 
    self.toolbarItems = toolbarItemsMutableArray; 
    //[self.navigationController.toolbar setItems: toolbarItemsMutableArray animated:YES]; 

    NSLog(@"toolbar %i", self.toolbarItems.count); 
}https://stackoverflow.com/questions/ask 

這就是NSLog的是打印:

Catalogue[361:10403] toolbar 4 
Catalogue[361:10403] toolbar item 0 <UIBarButtonItem: 0x8a24650> 
Catalogue[361:10403] toolbar item 1 <UIBarButtonItem: 0x8a36cd0> 
Catalogue[361:10403] toolbar item 2 <UIBarButtonItem: 0x8a36d30> 
Catalogue[361:10403] toolbar item 3 <UIBarButtonItem: 0x8a382f0> 
Catalogue[361:10403] toolbar 5 

這是我如何解決它:

我不得不alloc和初始化一個UILabel內功能,而不是使用伊娃。

UILabel *notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)]; 
    notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; 
    notificationsLabel.backgroundColor = [UIColor clearColor]; 
    //self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0]; 
    notificationsLabel.textColor = [UIColor whiteColor]; 
    notificationsLabel.text = @"Checking server"; 
    notificationsLabel.textAlignment = UITextAlignmentCenter; 
+0

NSLog的打印? – wizH 2012-03-02 06:46:13

+0

@wizH是的,他們是。我可以點擊按鈕,沒有文字。 :) – acecapades 2012-03-02 07:00:04

回答

3

我確信這段代碼會幫助你。記住代碼的同時更改代碼。我對這個工作,它工作正常,

UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 
[self.view addSubview:toolBar]; 

UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f,  self.view.frame.size.width, 21.0f)]; 
[titleLbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]]; 
[titleLbl setBackgroundColor:[UIColor clearColor]]; 
[titleLbl setTextColor=[UIColor blueColor]; 
[titleLbl setText:@"Title"]; 
[titleLbl setTextAlignment:UITextAlignmentCenter]; 

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:titleLbl]; 
NSArray *toolbarItemArr = [NSArray arrayWithObjects:button, nil]; 
[toolBar setItems:toolbarItemArr animated:YES]; 

爲了更好地理解可以按照以下鏈接: UIToolBar Class ReferenceUIBarButtonItem Class Reference

+0

當然。我會試試這個,我會讓你知道..事情是我只想編輯現有的工具欄項目,而不是alloc-init一個新的,然後將其添加到那裏。但我會試試這個。 – acecapades 2012-03-03 03:11:05

+0

嗨@acecapades絕對這將工作,因爲你試圖刪除工具欄項目數組的對象和分配一個新的對象,所以所有的邏輯是相同的。但是你必須在你的代碼中仔細實現這些代碼。我希望你能找到你的答案。祝你好運! – 2012-03-03 19:35:43

+0

嗨@acecapades我想你得到你的答案謝謝接受我的答案..! – 2012-03-04 10:37:02

2

我知道這個問題的回答,老,但我有同樣的經歷,並發現一些有趣的事情。我的代碼需要顯示(在工具欄中)一個按鈕,有時還會顯示一些標籤,具體取決於程序的狀態。如果我創造我的控制器類一個UILabel對象,並在調用重用,以

UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView: _myUILabel]; 
[self setToolbarItems: [NSArray arrayWithObject: myItem]]; 
[myItem release]; 
每次我需要從按鈕,將標籤切換時間

,則標籤將不會出現(事實上它顯示了第一次,在來回切換一次之後,它會停止顯示)。我在這個問題上花了幾個小時,如果不是因爲我禿頂的事實,我可能無論如何都把我所有的頭髮都放在絕望中。我覈實了我所有的保留計數都沒有問題。我保存的標籤非常有效。

我發現如果我每次需要在工具欄中顯示它時創建一個新標籤,那麼問題就會消失。在處理setToolbarItems中的UILabel時肯定存在一個問題。所以,我的建議,創建一個新的標籤,設置文本等,將其包裝在UIToolBarItem中,並再次設置您的工具欄項目。

這並不理想,它會執行alloc和release,但是它可以工作。我希望這會幫助那裏的人。