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

Iphone 如何安全地管理要从UI中删除的按钮?,iphone,ios,uibutton,target-action,Iphone,Ios,Uibutton,Target Action,我正在制作一个有点像iBooks的应用程序。屏幕上有一些内容,每个项目都由一个小缩略图表示。我希望用户能够像在iBooks中点击“编辑”按钮一样删除项目-出现X,项目被删除。我正在使用委托模式来处理所有这些问题,下面是一些代码: // Button is created in CustomView.h class UIImage *deleteImage = [UIImage imageNamed:@"delete.png"]; UIButton *button = [UIButton butt

我正在制作一个有点像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\u unhighlight]:发送到解除分配实例0x5f4a740的消息


我想发生的是,当调用我的目标操作实现时,释放按钮还不安全,就像我在委托方法中所做的那样。所以我的问题是,有什么更好的方法可以让may应用程序不崩溃?我想知道最干净的方法

如果CollectionOffcustomViews是唯一保留customView的对象,则在删除它时将其释放,因此它无法响应removeFromSuperview

切换这些代码行的顺序不会改变我遇到的问题。不确定这是否与此相关。是否在属性self.deleteButton上设置了保留?是的。它几乎是(非原子的,保留的)。哇,也许我应该在回答这个问题之前检查一下我的源代码。由于我仍在使用XCode 3.x,我为UIButton复制并粘贴了一个@property语句,我愚蠢地为BOOL属性复制并粘贴了它,所以它是赋值的,我只是浪费了最后4个小时哦,谢谢Ben.)