你能给我解释一下管理UIViewController生命周期的正确方式吗?
特别地,我想知道如何使用初始化,ViewDidLoad, ViewWillAppear, ViewDidAppear, ViewWillDisappear, ViewDidDisappear, ViewDidUnload和Dispose方法在UIViewController类的Mono Touch。
你能给我解释一下管理UIViewController生命周期的正确方式吗?
特别地,我想知道如何使用初始化,ViewDidLoad, ViewWillAppear, ViewDidAppear, ViewWillDisappear, ViewDidDisappear, ViewDidUnload和Dispose方法在UIViewController类的Mono Touch。
当前回答
当你加载/显示/隐藏视图控制器时,iOS会在适当的时间自动调用所有这些命令。需要注意的是这些方法附加到UIViewController而不是uiview本身。仅仅使用UIView你不会得到这些特性。
苹果网站上有很好的文档。简单地说:
ViewDidLoad - Called when you create the class and load from xib. Great for initial setup and one-time-only work. ViewWillAppear - Called right before your view appears, good for hiding/showing fields or any operations that you want to happen every time before the view is visible. Because you might be going back and forth between views, this will be called every time your view is about to appear on the screen. ViewDidAppear - Called after the view appears - great place to start an animations or the loading of external data from an API. ViewWillDisappear/DidDisappear - Same idea as ViewWillAppear/ViewDidAppear. ViewDidUnload/ViewDidDispose - In Objective-C, this is where you do your clean-up and release of stuff, but this is handled automatically so not much you really need to do here.
其他回答
在官方文档中解释状态转换:https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/index.html
这张图片显示了各种视图' will '和' did '回调方法之间的有效状态转换
有效的状态转换:
取自:https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Art/UIViewController类Reference_2x.png
海德尔的答案适用于ios 6之前的版本。然而,在iOS 6中,viewDidUnload和viewWillUnload从未被调用。文档声明:“视图在低内存条件下不再被清除,因此该方法永远不会被调用。”
当你加载/显示/隐藏视图控制器时,iOS会在适当的时间自动调用所有这些命令。需要注意的是这些方法附加到UIViewController而不是uiview本身。仅仅使用UIView你不会得到这些特性。
苹果网站上有很好的文档。简单地说:
ViewDidLoad - Called when you create the class and load from xib. Great for initial setup and one-time-only work. ViewWillAppear - Called right before your view appears, good for hiding/showing fields or any operations that you want to happen every time before the view is visible. Because you might be going back and forth between views, this will be called every time your view is about to appear on the screen. ViewDidAppear - Called after the view appears - great place to start an animations or the loading of external data from an API. ViewWillDisappear/DidDisappear - Same idea as ViewWillAppear/ViewDidAppear. ViewDidUnload/ViewDidDispose - In Objective-C, this is where you do your clean-up and release of stuff, but this is handled automatically so not much you really need to do here.
iOS 10,11 (Swift 3.1,Swift 4.0)
根据UIKit开发者的UIViewController,
1. loadView ()
如果子类没有使用nib,就应该在这里创建自定义视图层次结构。不要直接打电话。
2. loadViewIfNeeded ()
如果视图控制器的视图尚未被设置,则加载视图控制器的视图。
3. 视图加载()
在视图加载后调用。对于在代码中创建的视图控制器,它位于-loadView之后。对于从nib未归档的视图控制器,这是在视图被设置之后。
4. viewWillAppear(_ animated: Bool)
当视图即将变为可见时调用。违约不起任何作用
5. viewWillLayoutSubviews ()
在视图控制器的视图的layoutSubviews方法被调用之前调用。子类可以根据需要实现。违约不起任何作用。
6. 视图布局子视图()
在视图控制器的视图的layoutSubviews方法被调用之后调用。子类可以根据需要实现。违约不起任何作用。
7. viewDidAppear(_ animated: Bool)
当视图完全转换到屏幕上时调用。违约不起任何作用
8. viewWillDisappear(_ animated: Bool)
当视图被解散、覆盖或以其他方式隐藏时调用。违约不起任何作用
9. viewDidDisappear(_ animated: Bool)
在视图被解散,覆盖或以其他方式隐藏后调用。违约不起任何作用
10. viewWillTransition(大小:CGSize,协调器:UIViewControllerTransitionCoordinator)
视图正在转换时调用。
11. willMove(到父视图控制器:UIViewController?)
12. didMove(to arentviewcontroller parent: UIViewController?)
这两个方法是公共的,供容器子类在子控制器之间转换时调用。如果它们被覆盖,覆盖应确保调用超级。
当子对象从父对象中移除时,这两个方法中的父对象参数都为nil;否则它等于新的父视图控制器。
13. didReceiveMemoryWarning ()
当父应用程序接收到内存警告时调用。在iOS 6.0上,默认情况下它将不再清除视图。
图中没有提到viewWillLayoutSubviews和viewDidLayoutSubviews方法,但是在viewWillAppear和viewDidAppear之间调用这些方法。它们可以被多次调用。