ios UIView - 如何在加载视图时收到通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4501974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIView - How to get notified when the view is loaded?
提问by aryaxt
Is there anything similar to the viewDidLoad
of UIViewController
for a UIView
???
I need to be notified as soon as a UIView
has loaded (Subclass of UIView
), and perform some actions.
有什么类似viewDidLoad
的UIViewController
了UIView
??? 我需要在UIView
加载( 的子类UIView
)后立即收到通知,并执行一些操作。
回答by Costique
Depending on what kind of actions you need to perform, there are several techniques:
根据您需要执行的操作类型,有几种技术:
-(id)initWithFrame:(CGRect)frame
- UIView's designated initializer; always sent to a UIView to initialize it, unless the view is loaded from a nib;-(id)initWithCoder:(NSCoder *)coder
- always sent to initialize a UIView whenever the view is loaded from a nib;-(void)awakeFromNib
- sent after all the objects in the nib are initialized and connected; applicable only if you load the object from a nib; you must call super;-(void)willMoveToSuperview:(UIView *)newSuperview
- sent immediately before the view is added as a subview to another view;newSuperview
may be nil when you remove the view from its superview;-(void)willMoveToWindow:(UIWindow *)newWindow
- sent immediately before the view (or its superview) is added to a window;newWindow
may be nil when you remove the view from a window;-(void)didMoveToSuperview
- sent immediately after the view is inserted into a view hierarchy;-(void)didMoveToWindow
- sent immediately after the view gets its window property set. -
-(id)initWithFrame:(CGRect)frame
- UIView 的指定初始化器;总是发送到 UIView 来初始化它,除非视图是从笔尖加载的;-(id)initWithCoder:(NSCoder *)coder
- 每当从笔尖加载视图时,总是发送以初始化 UIView;-(void)awakeFromNib
- 在nib中的所有对象初始化并连接后发送;仅当您从笔尖加载对象时才适用;你必须打电话给超级;-(void)willMoveToSuperview:(UIView *)newSuperview
- 在视图作为子视图添加到另一个视图之前立即发送;newSuperview
当您从其超级视图中删除视图时可能为零;-(void)willMoveToWindow:(UIWindow *)newWindow
- 在视图(或其父视图)添加到窗口之前立即发送;newWindow
当您从窗口中移除视图时可能为零;-(void)didMoveToSuperview
- 在视图插入视图层次结构后立即发送;-(void)didMoveToWindow
- 在视图设置其窗口属性后立即发送。——
Basically, you can choose to perform your actions during initialization (1 & 2), after loading from a nib (3), before insertion into a view hierarchy (4 & 5) and after that (6 & 7).
基本上,您可以选择在初始化期间 (1 & 2)、从笔尖加载之后 (3)、插入视图层次结构之前 (4 & 5) 以及之后 (6 & 7) 执行您的操作。