UIViewControl生命周期

102118034803322
UIViewControl也许是我们在 UIKit 框架里面,用的最多的一个类了。了解每个步骤需要做什么工作很重要。
– (instancetype)init
初始化函数,初始化一些属性参数。
– (void)loadView;// This is where subclasses should create their custom view hierarchy if they aren’t using a nib. Should never be called directly.
当从非 nib 加载界面,同时 view 需要自定义时,要重载该函数。
可以同步执行一些准备工作,如获取数据。
– (void)viewDidLoad;// Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.
从非 nib 加载的界面,该函数表明加载界面的过程已经完成。
在其中可以做一些加载View之后的操作。
– (void)viewWillUnloadNS_DEPRECATED_IOS(5_0,6_0)__TVOS_PROHIBITED;
– (void)viewDidUnload NS_DEPRECATED_IOS(3_0,6_0) __TVOS_PROHIBITED; // Called after the view controller’s view is released and set to nil. For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.
当该 UIViewControl 非当前显示时,如果有内存警告出现,会调用该函数。
当该 UIViewControl 的view被置空释放之后,会调用该函数。
注意,无需手工在 dealloc 函数中调用该函数。
– (void)didReceiveMemoryWarning;// Called when the parent application receives a memory warning. On iOS 6.0 it will no longer clear the view by default.
当 UIViewControl 收到内存警告时候调用。在该函数中,应该做以下操作:
释放包含的 UIView。
释放属性并置空。
– (void)viewWillAppear:(BOOL)animated;   // Called when the view is about to made visible. Default does nothing
UIView 即将加载,处理一些view加载之前的操作,如绑定数据等。
– (void)viewDidAppear:(BOOL)animated;    // Called when the view has been fully transitioned onto the screen. Default does nothing
view 已经展现出来,可以做一些动画处理。
– (void)viewWillDisappear:(BOOL)animated;// Called when the view is dismissed, covered or otherwise hidden. Default does nothing
此时 view仍在展示中,可以做一些动画效果。
– (void)viewDidDisappear:(BOOL)animated; // Called after the view was dismissed, covered or otherwise hidden. Default does nothing
此时 view 已经消失,可以在后台改变其内容,不会被用户察觉到。