从 NIB 实例化时会调用 UIView 的什么方法?
我有一个简单的自定义视图,通过插座连接到 NIB。对于这个特定的视图,我想在初始化视图时对其执行一些操作,无论它位于哪个 NIB 上。
问题是, (id)init 或 (id)initWithFrame:(CGRect)frame 方法都没有在自定义视图上调用。
从 NIB 实例化 UIView 时会调用哪个方法?我只会使用视图控制器和 viewDidLoad 方法,除了这个特定的视图出现在许多不同的 NIB 上。
I have a simple custom view that is connected via outlet to a NIB. For this particular view, there are actions that I would like to perform on the view when it is initialized, no matter what NIB it is on.
Trouble is, neither the (id)init or the (id)initWithFrame:(CGRect)frame methods are getting called on the custom view.
Which method gets called on a UIView when it is instantiated from a NIB? I would just use the view controller and viewDidLoad method except that this particular view appears on a lot of different NIBs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
awakeFromNib
进行此类初始化。当 IB 实际创建对象并使用 NSCoding 归档对象时,会调用常规初始化方法,因此永远不会在您的应用程序中调用这些方法。您还可以重写将被调用的initWithCoder:
,但我不建议这样做,因为此时其他插座可能尚未连接。You can use
awakeFromNib
for this kind of initialization. The regular initialization methods are called when the object is actually created by IB and then archived usingNSCoding
, so those methods are never called within your application. You could also overrideinitWithCoder:
which will be called, but I don't recommend it since other outlets may not be wired at that point.