2012-03-02 98 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,但是它可以工作。我希望这会帮助那里的人。