gpt4 book ai didi

iphone - 我需要在 dealloc 方法中将 release 发送到我的实例变量吗? (iOS)

转载 作者:行者123 更新时间:2023-11-29 04:23:25 25 4
gpt4 key购买 nike

在我的类 dealloc 方法中,我有

- (void) dealloc
{
[searchField release];
[super dealloc];
}

其中 searchField 是在类变量中定义的。

@interface SearchCell : UITableViewCell
{
UISearchBar *searchField;
id delegate;
}

该类的使用方式如下:

if (indexPath.section == 0)
{
SearchCell *mycell = [[SearchCell alloc] init];
[cell setDelegate:self];
return [mycell autorelease];
}

searchField 在这里创建:

- (id) init
{
self = [super initWithFrame:CGRectZero];

[self create];

return self;
}

- (void) create
{
searchField = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
[self addSubview:searchField];
}

我需要使用[searchField release];在我的dealloc 中?应用程序崩溃并显示消息:“*[UISearchBar respondsToSelector:]: 消息已发送到已释放的实例 *”。

最佳答案

不,不要在那里发布它。由于您使用“autorelease”返回它,因此下一次运行事件循环将自动减少保留计数。

当您最终调用“dealloc”方法时,保留计数已经为 0,您的应用将引发异常。

关于iphone - 我需要在 dealloc 方法中将 release 发送到我的实例变量吗? (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com