IOS之从storyBoard生成的controller对象的生命周期问题
2014-09-04 19:56
260 查看
假设在storyBoard中共有两个控制器。
1.rootViewController :根控制器
2.testViewController: 测试用的控制器,它的storyBoardID设置为test
根控制器中得代码:
测试控制器中代码:
结论:
每次显示出的测试控制器对象的内存地址都不一样,这证明每次都会生成新的对象。
而当测试控制器消失时,在显示它的引用计数,则程序直接崩溃,证明test对象所占用的内存已被系统回收。
由此可知:test对象由[self.storyboard instantiateViewControllerWithIdentifier:@"test"]这个方法诞生。
在[rootViewController presentViewController:test animated:YES completion:NULL];这个方法显示出来。
最终由[self dismissViewControllerAnimated:YES completion:NULL];这个方法结束生命周期,test对象被系统回收。
1.rootViewController :根控制器
2.testViewController: 测试用的控制器,它的storyBoardID设置为test
根控制器中得代码:
#import "rootViewController.h" #import "testViewController.h" @interface test1ViewController () { testViewController *test; //测试控制器 } @end @implementation rootViewController ... //根控制器添加的按钮方法,点击按钮,就会弹出测试控制器界面 - (IBAction)buttonTapped:(id)sender { //用storyBoard的方法生成对象 test=[self.storyboard instantiateViewControllerWithIdentifier:@"test1"]; //动态显示出来 [self presentViewController:test animated:YES completion:NULL]; //打印出对象在内存中的地址 NSLog(@"%p",test); } //当测试控制器消失时用于检测test对象是否被系统回收。 - (IBAction)showTestRetainCount:(id)sender { NSLog(@"%d",[test RetainCount]); } @end
测试控制器中代码:
@implementation testViewController //添加一个返回按钮响应方法 - (IBAction)buttonTapped:(id)sender { [self dismissViewControllerAnimated:YES completion:^{ //当界面消失后打印出自己本身的引用计数,此时该对象虽然不在界面显示,但还没有被系统回收,引用计数不为0 NSLog(@"%d",[self retainCount]); }]; } @end
结论:
每次显示出的测试控制器对象的内存地址都不一样,这证明每次都会生成新的对象。
而当测试控制器消失时,在显示它的引用计数,则程序直接崩溃,证明test对象所占用的内存已被系统回收。
由此可知:test对象由[self.storyboard instantiateViewControllerWithIdentifier:@"test"]这个方法诞生。
在[rootViewController presentViewController:test animated:YES completion:NULL];这个方法显示出来。
最终由[self dismissViewControllerAnimated:YES completion:NULL];这个方法结束生命周期,test对象被系统回收。
相关文章推荐
- iOS开发内存警告Memory Warning和ViewController的生命周期的问题
- IOS开发storyboard创建controller黑屏以及控件不显示问题。
- iOS开发内存警告Memory Warning和ViewController的生命周期的问题
- iOS ARC下内存管理使用非自己生成对象的相关问题
- [ IOS ] 视图控制对象ViewController的生命周期
- 解决在 iOS 8 中用Storyboard modal UINavigationController不能dismiss的问题
- 第六天 iOS中用Storyboard modal UINavigationController不能dismiss的问题
- iOS App内存优化之 解决UIImagePickerController的图片对象占用RAM过高问题
- iOS开发笔记3——IOS开发内存警告Memory Warning和ViewController的生命周期的问题
- iOS开发内存警告Memory Warning和ViewController的生命周期的问题
- iOS开发内存警告Memory Warning和ViewController的生命周期的问题
- iOS学习-删除storyboard中viewController黑屏问题
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (2) 代码部分
- UIViewController的生命周期及iOS程序执行顺序
- java书写规范问题,for、while、do while{}后面不能直接定义变量或生成对象(在没有大括号的情况下)
- IOS开发:一个关于UIPopoverController设置contentsize大小的问题
- IOS UINavigationController的下级页面(子页面)更改titleView不显示的问题
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (1)
- 关于dtree生成对象嵌入原网页的问题
- 关于linq 的dbml自动生成的表对象带s和不带s的问题