我在Interface中添加了一个自定义的UIView,并向我的视图控制器添加了一个出口,我希望在加载视图之前设置一些条件属性。我该怎么做?
我尝试在以下方法中设置属性:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
获取在初始化器之前调用的UIView,但是视图控制器中的出口此时会记录(null)
。
- (void)viewDidLoad
此时,出口已经指向我的UIView,但是这个方法在( UIView初始化)之后被称为。
我可以为UIView编写一个自定义的初始化器,但是如何从我的viewController (而不是?)调用这个初始化器?界面生成器初始化它?
发布于 2014-12-19 08:38:29
通常,您可以在视图的layoutSubviews方法中进行这种设置。
以下是事件的顺序:
如果该事件序列与您正在执行的操作的时间不匹配,您还可以在更新属性之后尝试在视图上调用setNeedsDisplay。这将导致在下一个显示周期调用视图的layoutSubviews方法。
发布于 2014-12-19 07:40:05
为此,您可能可以重写awakeFromNib
方法。
来自文档
awakeFromNib
消息:- In OS X, this message is sent to any interface objects that define the method. It is also sent to the File’s Owner and any placeholder objects that define it as well.
- In iOS, this message is sent only to the interface objects that were instantiated by the nib-loading code. It is not sent to File’s Owner, First Responder, or any other placeholder objects.
https://stackoverflow.com/questions/27569179
复制相似问题