iOS,UIBarButtonItem:绑定到UIBarButtonItem的自定义标签

时间:2014-11-12 17:57:25

标签: ios cocoa-touch uibarbuttonitem

我在附加绑定到我的UIBarButtonItem的自定义标签时遇到问题。我的代码:

UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0.00, 0.00, 10.0, 10.0)];
        lbl.font=[UIFont fontWithName:@"Verdana" size:7];
        lbl.textAlignment=NSTextAlignmentCenter;
        lbl.textColor=[UIColor darkGrayColor];
        lbl.text=@"Subtypes available:";
        lbl.backgroundColor=[UIColor lightGrayColor];

        _btnSubtypes.enabled=YES; // UIBarButtonItem
        _btnSubtypes.title=[NSString stringWithFormat:@"%lu / %lu",[_ucqc returnQuantityOfListedSubtypes:_coin],[_ucqc returnPureQuantityOfSubtypesInType:_coin]];
        [_btnSubtypes setAction:@selector(navigateToSubtypes)];
        [_btnSubtypes.customView addSubview:lbl];

此代码以viewWillAppear方式执行。所以,它没有设置标签。怎么了?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

嗨,我认为为了自定义使用UIButton而不是UIBarButtonItem更好。在您的代码中,您没有任何customView。如果你插入一个NSLog语句,你可以看到比nil。

[_btnSubtypes.customView addSubview:lbl];
NSLog(@"have I go customView? %@",[_btnSubtypes.customView description]);

你应该改变这个:

  _btnSubtypes.customView=lbl;

但这可能与你期望的结果不符。

相关问题