从NIB实例化时调用UIView的方法是什么? [英] What method of UIView gets called when instantiated from a NIB?

查看:129
本文介绍了从NIB实例化时调用UIView的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的自定义视图,通过插座连接到NIB。对于这个特定的视图,我想在视图初始化时执行操作,无论它在哪个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.

麻烦,既不是(id)init或(id)initWithFrame:(CGRect)框架方法在自定义视图上被调用。

Trouble is, neither the (id)init or the (id)initWithFrame:(CGRect)frame methods are getting called on the custom view.

在实例化UIView时调用哪个方法来自NIB?我只会使用视图控制器和viewDidLoad方法,除了这个特定的视图出现在很多不同的NIB上。

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.

推荐答案

你可以使用 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 using NSCoding, so those methods are never called within your application. You could also override initWithCoder: which will be called, but I don't recommend it since other outlets may not be wired at that point.

这篇关于从NIB实例化时调用UIView的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆