gpt4 book ai didi

iphone - 我可以修复崩溃,但我不明白为什么会这样

转载 作者:可可西里 更新时间:2023-11-01 06:18:33 25 4
gpt4 key购买 nike

我有一个 ScrollView 。我向这个 ScrollView 添加一个按钮并在之后释放它。

UIButton * saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
saveButton.frame = CGRectMake(415.0, 473, 80, 38);
saveButton.titleLabel.font = [UIFont fontWithName:@"Heiti TC" size:24];
[saveButton setTitle:@"" forState:UIControlStateNormal];
[saveButton setContentEdgeInsets:UIEdgeInsetsMake(2, 0, 0, 0)];
saveButton.backgroundColor = [UIColor clearColor];
[saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
[saveButton setBackgroundImage:[UIImage imageNamed:@"save.png"] forState:UIControlStateNormal];
[saveButton addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
saveButton.hidden = NO;
[self.scrollview addSubview:saveButton];
[saveButton release];

当 View 出现在屏幕上时应用程序崩溃,我尝试触摸屏幕的任何部分。

如果我注释掉

[saveButton release];

应用程序完美运行。

我认为一旦将按钮添加到 ScrollView ,按钮的保留计数就会增加,这样我就可以安全地释放按钮。

这是怎么回事?将某些内容添加到 ScrollView 与将其添加到主视图(如下所示)是否不同?

[self.view addSubview:saveButton];

最佳答案

buttonWithType: 是一个方便的构造函数,所以它已经创建了一个自动释放的实例,不需要释放对象。

这意味着下面这行代码是错误的:

[saveButton release];

您不应发送release,因为实例已经自动释放。

检查 UIButton reference了解详情。

关于iphone - 我可以修复崩溃,但我不明白为什么会这样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10683852/

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