我试图改变.xib中按钮的标题我加载为:
CustomToolBar *toolBar = (CustomToolBar *)[[[[NSBundle mainBundle]
loadNibNamed:@"CoolBar"
owner:self options:nil] objectAtIndex:0]
initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 70)];
我尝试使用
直接更改它toolBar.button.titleLabel.text = @"VALUE";
通过自定义setter方法,
- (void)updateButtonText:(NSString *)buttonText {
_button.titleLabel.text = buttonText;
}
但是,它始终默认为.xib文件中给定的值。
答案 0 :(得分:1)
CustomToolBar *toolBar =
(CustomToolBar *)[[[[NSBundle mainBundle]
loadNibNamed:@"CoolBar"
owner:self options:nil] objectAtIndex:0]
initWithFrame:CGRectMake(
0, self.view.frame.size.height, self.view.frame.size.width, 70)];
你的代码毫无意义。你不能/不能init
从笔尖加载的对象;它已经初始化了。
我建议您将该声明分成几个步骤,并确保每个步骤都能为您提供所期望的结果 - 当然,放弃init
。
NSArray* arr = [NSBundle mainBundle]
loadNibNamed:@"CoolBar"
owner:nil options:nil]; // did it load?
id obj = [arr objectAtIndex: 0]; // did you get the CustomToolbar?
CustomToolBar *toolBar =
(CustomToolBar *) obj; // still ok?
toolbar.frame = CGRectMake(
0, self.view.frame.size.height, self.view.frame.size.width, 70);
等等。
然后,您可以开始考虑使用toolBar
做的内容。目前它没有被放入您的界面,因此您无法知道您的更改是否正在影响它。完成后,您可以开始问自己代码的 rest 。例如,尝试直接设置UIButton的titleLabel.text
是疯狂的(和错误的);这不是一个按钮的工作方式。获得toolBar
后,您可以说
[toolBar.button setTitle:@"VALUE" forState: UIControlStateNormal];
答案 1 :(得分:0)
而不是像使用CustomToolBar *toolBar = [[CustomToolBar alloc] initWithNibNamed:@"CoolBar" inBundle:nil]
发送nil加载默认包,这是您的主要包。
您可以在完成后更改框架。