在dealloc方法中,需要或不需要将任何委托设置为nil [英] In dealloc method set any delegate to nil is needed or not needed

查看:146
本文介绍了在dealloc方法中,需要或不需要将任何委托设置为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过如下所示的程序化在视图中创建了tableview

I have created tableview in my view by programmatic like below

table = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 320, 370) style:UITableViewCellStyleDefault];
table.delegate = self;
table.dataSource = self;
table.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:table]; 

在dealloc方法中,我的写法如下

in dealloc method i write like below

table.delegate = nil;
 table.dataSource = nil;
[table release];
table=nil;

这是更好的方法,还是下面的方法更好

this the better way or below one is better

[table release];
table=nil;

我想知道我是否不重置委托和dataSource会发生什么情况

I want to know if i dont reset delegate and dataSource what will happen

谢谢

推荐答案

如果要将充当委托的对象分配给其他对象,则需要确保在调用<前将其委托设置为nil. c0>(假设对象不保留其委托的正常模式).这是因为,当[super dealloc]返回时,此对象不再是有效对象,并且如果未将其设置为nil,则它是有效具有悬挂引用的委托的对象.

If you are deallocating an object that acts as the delegate to other objects, you need to make sure that you have set their delegates to nil, before you call [super dealloc] (assuming the normal pattern that objects do not retain their delegates). This is because when [super dealloc] has returned, this object is no longer a valid object and the objects it is a delegate to effectively have dangling references, if they have not been set to nil.

在这种特殊情况下,您可能会不做而逃脱,因为除非拆除UI且表视图不再需要使用其委托或数据源,否则可能不会调用对象的dealloc.别打赌.

In this particular case, you would probably get away without doing it because your object's dealloc probably won't get called except when the UI is being dismantled and the table view no longer needs to use its delegate or data source, but don't bet on it.

这篇关于在dealloc方法中,需要或不需要将任何委托设置为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆