【问题标题】:Title in navbar crashing because of a message being sent to a deallocated instance由于将消息发送到已释放的实例,导航栏中的标题崩溃
【发布时间】:2012-10-18 15:07:01
【问题描述】:

我在viewDidLoad 中有以下代码(用于在导航栏中设置标题),该代码因“发送到已释放实例的消息”错误而崩溃:

UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];

label.textColor = [UIColor whiteColor];
label.backgroundColor=[UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20];
label.font = [UIFont fontWithName:@"Mayfield Regular" size:15];
self.navigationItem.titleView = label;

label.text=@"SEARCH"; //CUSTOM TITLE
[label sizeToFit];
[label release];

我该如何解决这个问题?

感谢您的帮助

【问题讨论】:

  • 删除autorelease[label release]

标签: objective-c ios memory-management


【解决方案1】:

您过度释放标签。您在发布的第一行中调用autorelease,然后在发布的最后一行中也调用release。只做其中一项。

【讨论】:

  • 我现在明白了。这是一个旧项目,只是对其进行了一些小改动。谢谢!
【解决方案2】:
UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];

然后释放

[label release];

这显然是错误的。你要么选择自动发布,要么选择发布。

【讨论】:

    猜你喜欢
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 2011-06-16
    相关资源
    最近更新 更多