您的位置:首页 > 其它

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

2012-07-16 01:08 816 查看
刚调试程序时发现一个很诡异的问题,我从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()。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐