iOS----------viewcontroller中的dealloc方法不调用

ios的viewcontroller生命周期是 init -> loadView -> viewDidLoad -> viewWillAppear -> viewDidAppear -> viewWillDisappear -> viewDidAppear -> viewDidUnload -> dealloc

当我们使用NSNotificationCenter方法,可以在viewDidLoad中添加监听,在dealloc中remove监听,但是有的时候我们发现,在退出这个vc的时候,系统不调用dealloc方法,造成这样的方法可能有以下几种原因(目前知道的):

 

1、viewcontroller中存在定时器NSTimer

  [self.timer invalidate];//结束定时

 

  self.timer = nil;//nil

 

2、viewcontroller中有代理Delegate,需要设置delegate的时候,设置为weak

 

@property (nonatomic,weakid<WorkHistoryDelegate>delegate;

 

3、viewcontroller中有Block方法

block会把它里面的所有对象强引用,包括当前控制器self,因此有可能会出现循环引用的问题。比如viewController中有个block属性,在block中又强引用了self或者其他成员变量,那么这个viewController与自己的block属性就形成循环引用,导致viewController无法释放。

 

    __weak typeof(self) weakSelf = self;

    [self.tableView tableViewAddUpLoadRefreshing:^{

 

[weakSelf loadCommentListData];

 

    }];

posted @ 2018-07-30 10:43  iOS张文权  阅读(1097)  评论(0编辑  收藏  举报