你能给我解释一下管理UIViewController生命周期的正确方式吗?
特别地,我想知道如何使用初始化,ViewDidLoad, ViewWillAppear, ViewDidAppear, ViewWillDisappear, ViewDidDisappear, ViewDidUnload和Dispose方法在UIViewController类的Mono Touch。
你能给我解释一下管理UIViewController生命周期的正确方式吗?
特别地,我想知道如何使用初始化,ViewDidLoad, ViewWillAppear, ViewDidAppear, ViewWillDisappear, ViewDidDisappear, ViewDidUnload和Dispose方法在UIViewController类的Mono Touch。
当前回答
根据苹果的文件-开始开发iOS应用程序(Swift) -使用视图控制器-理解视图控制器生命周期
viewDidLoad()—Called when the view controller’s content view (the top of its view hierarchy) is created and loaded from a storyboard. … Use this method to perform any additional setup required by your view controller. viewWillAppear()—Called just before the view controller’s content view is added to the app’s view hierarchy. Use this method to trigger any operations that need to occur before the content view is presented onscreen viewDidAppear()—Called just after the view controller’s content view has been added to the app’s view hierarchy. Use this method to trigger any operations that need to occur as soon as the view is presented onscreen, such as fetching data or showing an animation. viewWillDisappear()—Called just before the view controller’s content view is removed from the app’s view hierarchy. Use this method to perform cleanup tasks like committing changes or resigning the first responder status. viewDidDisappear()—Called just after the view controller’s content view has been removed from the app’s view hierarchy. Use this method to perform additional teardown activities.
其他回答
当你加载/显示/隐藏视图控制器时,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.
更新:ViewDidUnload在iOS 6中已弃用,因此更新了相应的答案。
UIViewController的生命周期如图所示:
使用Xamarin Native/Mono Touch的优势在于,它使用了原生api,因此它遵循与Apple文档中相同的ViewController生命周期。
海德尔的答案适用于ios 6之前的版本。然而,在iOS 6中,viewDidUnload和viewWillUnload从未被调用。文档声明:“视图在低内存条件下不再被清除,因此该方法永远不会被调用。”
在官方文档中解释状态转换: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应用程序(Swift) -使用视图控制器-理解视图控制器生命周期
viewDidLoad()—Called when the view controller’s content view (the top of its view hierarchy) is created and loaded from a storyboard. … Use this method to perform any additional setup required by your view controller. viewWillAppear()—Called just before the view controller’s content view is added to the app’s view hierarchy. Use this method to trigger any operations that need to occur before the content view is presented onscreen viewDidAppear()—Called just after the view controller’s content view has been added to the app’s view hierarchy. Use this method to trigger any operations that need to occur as soon as the view is presented onscreen, such as fetching data or showing an animation. viewWillDisappear()—Called just before the view controller’s content view is removed from the app’s view hierarchy. Use this method to perform cleanup tasks like committing changes or resigning the first responder status. viewDidDisappear()—Called just after the view controller’s content view has been removed from the app’s view hierarchy. Use this method to perform additional teardown activities.