2017年5月5日
一.強引用對象導(dǎo)致頁面push后,沒有調(diào)用dealloc對象
1.原因block里面用了強引用對象來操作計時器
- (void)initCountTimeView
{
if (_pageType == HuTestPracticePageTypeDoPaper) {
if(_examType == HuExamTypeExam){
if(_coutTimeV == nil){
NSDictionary *dic = @{fontColorKey:[UIColor blueColor]};
//test
// _reqParamM.timeNum = 600;
_coutTimeV = [[HuCountTimeView alloc]initWithTime:_reqParamM.timeNum AndDic:dic];
//設(shè)置絕對時間值
NSInteger nowTime = [HuConfigration getCurrentSeconds];
_absolutTime = nowTime + _reqParamM.timeNum;
//時間到了上傳答案
__block HuTestPracticeViewController *strongSelf = self;
_coutTimeV.timeOut = ^(){
[strongSelf uploadPaper:YES];
[strongSelf.coutTimeV endCountDown];
};
_coutTimeV.changeColorTimeOut = ^(){
strongSelf.coutTimeV.timeL.textColor = [HuConfigration uiColorFromString:@"#f66767"];
};
//頁面創(chuàng)建就啟動
[_coutTimeV startCountDown];
}
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_coutTimeV];
}
}
}
2.修改成如下就可以(目前不需要修改成員變量,所以其實不用__block)
- (void)initCountTimeView
{
if (_pageType == HuTestPracticePageTypeDoPaper) {
if(_examType == HuExamTypeExam){
if(_coutTimeV == nil){
NSDictionary *dic = @{fontColorKey:[UIColor blueColor]};
//test
// _reqParamM.timeNum = 600;
_coutTimeV = [[HuCountTimeView alloc]initWithTime:_reqParamM.timeNum AndDic:dic];
//設(shè)置絕對時間值
NSInteger nowTime = [HuConfigration getCurrentSeconds];
_absoluteTime = nowTime + _reqParamM.timeNum;
//時間到了上傳答案(需要修改局部變量)
WS(weakSelf);
_coutTimeV.timeOut = ^(){
[weakSelf uploadPaper:YES];
[weakSelf.coutTimeV endCountDown];
};
_coutTimeV.changeColorTimeOut = ^(){
weakSelf.coutTimeV.timeL.textColor = [HuConfigration uiColorFromString:@"#f66767"];
};
//頁面創(chuàng)建就啟動
[_coutTimeV startCountDown];
}
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_coutTimeV];
}
}
}
2016年10月19日
1.原因是循環(huán)引用和成員變量block內(nèi)嵌套使用導(dǎo)致
a.block中如果使用了__strong修飾的額對象變量,如self,那么改對象就會被block所持有,導(dǎo)致循環(huán)引用,dealloc沒有被釋放。
b.成員變量 _number 在第一次block使用后,又在第二層block使用

Paste_Image.png
2.解決修改 (改成弱引用,去掉多余的成員變量在blcok嵌套使用)
//對自己弱引用
#define WS(weakSelf) __weak typeof(self)weakSelf = self;
@interface MyFavoriteViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UILabel *_myLab;
int _number;
}
- (void)createTableView
{
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, HHBWIDTH,HHBHEIGHT-40-64-49-40) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.rowHeight=80;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone ;
[self.tableView registerNib:[UINib nibWithNibName:@"ListFavriteMapperTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
[self.view addSubview:self.tableView];
WS(weakSelf)
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
// _number = 0;
[weakSelf tableViewRefreshSelf];
}];
[self.tableView.mj_header beginRefreshing];
}
-(void)tableViewRefreshSelf
{
// _number = 0;
NSString *nurseId=[[NSUserDefaults standardUserDefaults]objectForKey:@"nurseId"];
NSDictionary *parameters=@{@"nurseId":nurseId};
WS(weakSelf)
[MpHttpTolls listFavrite:parameters success:^(NSArray *data) {
_number = 0;
//other action
} view:weakSelf.view];
}
- (void)dealloc
{
[kNotificationCenter removeObserver:self];
}
如果您發(fā)現(xiàn)本文對你有所幫助,如果您認(rèn)為其他人也可能受益,請把它分享出去。