在工具栏中添加自定义标签不起作用

时间:2012-03-02 06:38:40

标签: iphone objective-c xcode uinavigationcontroller

我正在尝试在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

这就是我解决它的方法:

我必须在函数内部分配和初始化UILabel,而不是使用ivar。

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;

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

我知道这个问题已经回答了,但我有同样的经历,并发现了一些有趣的东西。我的代码需要有时显示(在工具栏中)按钮,并根据程序的状态显示其他一些标签。如果我在我的控制器类中创建一个UILabel对象,并在调用

时重用它
UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView: _myUILabel];
[self setToolbarItems: [NSArray arrayWithObject: myItem]];
[myItem release];

每当我需要从按钮切换到标签时,标签就不会显示出来(实际上它第一次显示,而来回切换一次后,它就会停止显示)。我在这个问题上花了好几个小时,如果不是因为我是秃头,我可能会绝望地把所有的头发都拉下来。我核实了我的所有保留计数都没问题。我保存的标签很好且有效。

我发现如果每次我需要在工具栏中显示它时创建一个新标签,那么问题就会消失。在setToolbarItems中处理UILabel肯定存在问题。所以,我的建议,创建一个新标签,设置文本等,将其包装在UIToolBarItem中,并再次设置工具栏项。

这不是理想的,它会更多地运用alloc和release,但它确实有效。我希望这能帮助那里的人。

相关问题