问题描述
我看到了以下代码:
CoolButton *coolButton = [[CoolButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 60.0f, 25.0f)];
[coolButton awakeFromNib];
// ...
[coolButton release];
CoolButton
子类UIButton
.以编程方式调用awakeFromNib
可以吗?
CoolButton
subclasses UIButton
. Is it OK to call awakeFromNib
programmatically?
如果我要执行与-[CoolButton awakeFromNib]
中相同的操作,而不是通过nib文件来编程创建对象,那么最好的方法是什么?
What's the best way to do this if I want to do the same things that are done in -[CoolButton awakeFromNib]
for times that I want to create the object programmatically, instead of from a nib file?
我应该只定义一个方法并在-[CoolButton awakeFromNib]
和-[CoolButton initWithFrame]
中调用它吗?还是在这两种情况下都需要调用另一种初始化方法:我是通过编程方式还是通过笔尖文件创建CoolButton
?
Should I just define a method and call it in -[CoolButton awakeFromNib]
and -[CoolButton initWithFrame]
? Or, is there another initialization method I should define that gets called in both cases: whether I create a CoolButton
programmatically or from a nib file?
推荐答案
不,没有一种方法可以同时调用代码中创建的对象和NIB中创建的对象.您应该编写一个包含通用初始化逻辑的单独方法,并从awakeFromNib
和initWithFrame:
进行调用.
No, there isn't a method that gets called both for objects created in code and objects created from a NIB. You should write a separate method that contains the common initialization logic and call it from both awakeFromNib
and initWithFrame:
.
这篇关于UIKit:以编程方式调用awakeFromNib吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!