如何安全地内存管理要从 UI 中删除的按钮?

发布于 2024-09-26 08:53:25 字数 1187 浏览 3 评论 0原文

我正在制作一个有点像 iBooks 的应用程序。屏幕上有一些内容,每个项目都由一个小缩略图表示。我希望用户能够删除这些项目,就像您点击 iBooks 中的“编辑”按钮一样 - 出现一个 X,并且该项目被删除。我使用委托模式来处理所有这些,因此这里有一些代码:

// Button is created in CustomView.h class
UIImage *deleteImage = [UIImage imageNamed:@"delete.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
self.deleteButton = button;
[self.deleteButton setImage:deleteImage forState:UIControlStateNormal]; 
[self.deleteButton addTarget:self action:@selector(deleteIt) forControlEvents:UIControlEventTouchUpInside];

// Here's what's called when the delete button is pushed
- (IBAction)deleteMap {
[self.customViewDelegate itemWasDeleted:self];  
}

// And here's the implementation of that method, in a View Controller
- (void)itemWasDeleted:self:(CustomView*)customView {
    // delete domain object
    // . . .    

    [self.collectionOfCustomViews removeObject:customView];
    [customView removeFromSuperview];
}

这段代码的问题是我收到了错误访问异常。通过 NSZombie,看起来像这样:

* -[UIButton _unhighlight]:消息发送到已释放实例 0x5f4a740

我猜发生的情况是,当调用我的目标操作实现时,释放按钮尚不安全,正如我在中所做的那样我的委托方法。所以我的问题是,有什么更好的方法可以做到这一点,这样应用程序就不会崩溃?我想知道最干净的方法。

I'm making an app that is kinda like iBooks. There is some stuff on the screen, each item represented by a small thumbnail. I want the user to be able to delete the items much like when you tap the "Edit" button in iBooks - an X comes up, and the item is deleted. I'm using the delegation pattern to handle all of this, so here is some code:

// Button is created in CustomView.h class
UIImage *deleteImage = [UIImage imageNamed:@"delete.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
self.deleteButton = button;
[self.deleteButton setImage:deleteImage forState:UIControlStateNormal]; 
[self.deleteButton addTarget:self action:@selector(deleteIt) forControlEvents:UIControlEventTouchUpInside];

// Here's what's called when the delete button is pushed
- (IBAction)deleteMap {
[self.customViewDelegate itemWasDeleted:self];  
}

// And here's the implementation of that method, in a View Controller
- (void)itemWasDeleted:self:(CustomView*)customView {
    // delete domain object
    // . . .    

    [self.collectionOfCustomViews removeObject:customView];
    [customView removeFromSuperview];
}

The problem with this code is that I get a Bad Access Exception. Via NSZombie, looks like this:

* -[UIButton _unhighlight]: message sent to deallocated instance 0x5f4a740

I guess what's happening is that when my target-action implementation is called, it's not yet safe to release the button, as I'm doing in my delegate method. So my question is, what's a better approach to doing this so that may app doesn't crash? I'd like to know the cleanest approach possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2024-10-03 08:53:25

如果collectionOfCustomViews是唯一保留customView的对象,那么当您删除它时释放它,因此它无法响应removeFromSuperview。

If collectionOfCustomViews was the only object retaining customView then you released it when you removed it, so it can't respond to removeFromSuperview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文