IOS 控件的生命周期

ViewController的生命周期包括:
Initialize
ViewDidLoad
ViewWillAppear
ViewDidAppear
ViewWillDisappear
ViewDidDisappear
ViewDidUnload
Dispose

对于Application来说,ViewController的每个阶段都对应了一个方法,IOS会在适当的时间调用对应的方法,因此,我们可以在每个方法中添加对应的代码来做我们想做的事。需要注意的是,这些方法都是为ViewController服务的,对于Views对象来说,是没有这些方法的。
View的生命周期方法如下:

* 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.

* ViewWill/DidDisappear - Same idea as the WillAppear.

* ViewDidUnload/Dispose - Available to you, In objective-c, this is where you do your cleanup and release of stuff, but this is handled automatically so not much you really need to do here.

Application的生命周期包括:
在IOS中,生命周期就是由一系列的方法构成, 对于一个App来说,涉及到的生命周期有:
1、点击icon启动,对应的方法有:
didFinishLaunchingWithOptions:
applicationDidBecomeActive:
2、按下Home键,返回桌面,对应方法:
applicationWillResignActive
applicationDidBecomeInactive
3、再点击icon回到前台,对应方法:
applicationWillEnterForegroud
applicationDidBecomeActive
4、Application忽然被终止,对应方法:
applicationWillTerminate
这儿需要注意,点2下home键,把后台程序关掉不会调用这个方法,在苹果的IOS开发指南上有如下描述:
”Even if you develop your application using iPhone SDK 4 and later, you must still be prepared for your application to be terminated. If memory becomes constrained, the system might remove applications from memory in order to make more room. If your application is currently suspended, the system removes your application from memory without any notice. However, if your application is currently running in the background, the system does call the applicationWillTerminate:method of the application delegate. Your application cannot request additional background execution time from this method.“ 就是说,在机器内存不足时,IOS会清理后台程序,在这个时候会调用该方法,一般情况下,很少会用到这个方法,尽量不要在这个方法里写你的应用逻辑。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值