views:

32

answers:

1

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.

+1  A: 

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

Ben
Switching the order of those lines of code doesn't change anything with the problem I'm having. Not sure if that's relevant here.
bpapa
Do you have retain set on the property self.deleteButton?
Ben
Yep. It's pretty much (nonatomic,retain) for all.
bpapa
Wow maybe I should have checked my source code before answering that. Since I'm still using XCode 3.x I copied and pasted a @property statement for the UIButton, and I stupidly did it for a BOOL property, so IT WAS ASSIGN AND I JUST WASTED THE LAST 4 HOURS oh well thanks Ben. :)
bpapa