将 属性 分配给加载的 .xib
Assigning Property to Loaded .xib
我正在尝试更改正在加载的 .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 文件中给定的值。
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];
而不是像你使用的那样加载笔尖 CustomToolBar *toolBar = [[CustomToolBar alloc] initWithNibNamed:@"CoolBar" inBundle:nil]
发送 nil 会加载默认包,这是您的主包。
您可以在完成后更改其框架。
我正在尝试更改正在加载的 .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 文件中给定的值。
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];
而不是像你使用的那样加载笔尖 CustomToolBar *toolBar = [[CustomToolBar alloc] initWithNibNamed:@"CoolBar" inBundle:nil]
发送 nil 会加载默认包,这是您的主包。
您可以在完成后更改其框架。