-viewWillAppear:和 -viewDidAppear:区别

本文详细解释了iOS应用中视图控制器的三个关键生命周期方法:ViewDidLoad、ViewWillAppear及ViewDidAppear的功能与使用场景。ViewDidLoad主要用于加载视图并初始化控件;ViewWillAppear适合更新数据,避免在此创建UIView以免影响性能;而ViewDidAppear则适用于启动耗时任务如网络请求,并可在此显示等待提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 -viewWillAppear:和 -viewDidAppear:区别在于will和did的区别!

viewWillAppear是在视图出现之前调用,更新用于显示视图的信息,此时视图还没有appear。

viewDidAppear是在视图出现之后调用,用于更新视图显示出来后,一些动画之类的操作。

1) ViewDidLoad - Whenever I'm adding controls to a view that should appear together with the view, right away, I put it in the ViewDidLoad method. Basically this method is called whenever the view was loaded into memory. So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without those forms.

2) ViewWillAppear: I use ViewWillAppear usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation of UIViews is fairly expensive, and you should avoid as much as possible doing that on the ViewWillAppear method, becuase when this gets called, it means that the iPhone is already ready to show the UIView to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).If you override this method, you must call super at some point in your implementation.

3) ViewDidAppear: Finally, I use the ViewDidAppear to start off new threads to things that would take a long time to execute, like for example doing a webservice call to get extra data for the form above.The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data.If you override this method, you must call super at some point in your implementation.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值