首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Iphone >

(iPhone/iPad开发)解决viewController后退时不执行delloc()有关问题

2012-07-28 
(iPhone/iPad开发)解决viewController后退时不执行delloc()问题刚调试程序时发现一个很诡异的问题,我从Vie

(iPhone/iPad开发)解决viewController后退时不执行delloc()问题

刚调试程序时发现一个很诡异的问题,我从ViewController A push进 ViewController B,在从B back时发现程序不会执行B里面的delloc(),很诡异的问题,因为按理说此时点击back是执行pop操作的,是会执行delloc()函数的,但经调试发现确实没有执行。

后来经过google发现,The dealloc method was not being called if any of the references held by a viewcontroller were still in memory.也就是说,我当前View B 内存中计数器不为0,经排查发现,我是在ViewDidLoad函数里面开启了一个time类型,

timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];

这一句调用的时候对 target:self 进行了retain,这时候你pop回上一级,self的引用计数还剩1,所以铁定不会dealloc,我的解决办法是在- (void)viewWillDisappear:(BOOL)animated()函数里执行[timer invalidate];这样,self view计数器为0,当前view执行了delloc()。

1楼ccf070344分钟前
学习了。看来以后自己写的时候也要注意。

热点排行