想了解 iOS UIViewController 生命周期 [英] Looking to understand the iOS UIViewController lifecycle

查看:26
本文介绍了想了解 iOS UIViewController 生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释一下管理 UIViewController 生命周期的正确方式吗?

Could you explain me the correct manner to manage the UIViewController lifecycle?

特别想知道如何使用InitializeViewDidLoadViewWillAppearViewDidAppearViewWillDisappearViewDidDisappearViewDidUnloadDispose 方法在 Mono Touch 中用于 UIViewController 类.

In particular, I would like to know how to use Initialize, ViewDidLoad, ViewWillAppear, ViewDidAppear, ViewWillDisappear, ViewDidDisappear, ViewDidUnload and Dispose methods in Mono Touch for a UIViewController class.

推荐答案

当您加载/呈现/隐藏视图控制器时,iOS 会在适当的时间自动调用所有这些命令.需要注意的是,这些方法附加到 UIViewController 而不是 UIView 本身.仅使用 UIView,您将无法获得任何这些功能.

All these commands are called automatically at the appropriate times by iOS when you load/present/hide the view controller. It's important to note that these methods are attached to UIViewController and not to UIViews themselves. You won't get any of these features just using a UIView.

Apple 网站上有很棒的文档这里.简单地说:

There's great documentation on Apple's site here. Putting in simply though:

  • ViewDidLoad - 在创建类并从 xib 加载时调用.非常适合初始设置和一次性工作.

  • ViewDidLoad - Called when you create the class and load from xib. Great for initial setup and one-time-only work.

ViewWillAppear - 在您的视图出现之前调用,适用于隐藏/显示字段或您希望每次在视图可见之前发生的任何操作.由于您可能会在视图之间来回切换,因此每次您的视图即将出现在屏幕上时都会调用此方法.

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 - 在视图出现后调用 - 开始动画或从 API 加载外部数据的好地方.

ViewDidAppear - Called after the view appears - great place to start an animations or the loading of external data from an API.

ViewWillDisappear/DidDisappear - 与 ViewWillAppear/ViewDidAppear 相同.

ViewWillDisappear/DidDisappear - Same idea as ViewWillAppear/ViewDidAppear.

ViewDidUnload/ViewDidDispose - 在 Objective-C 中,这是你清理和释放东西的地方,但这是自动处理的,所以不是你真的需要在这里做很多事情.

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 UIViewController 生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆