Button causing EXC_BAD_ACCESS
I have trawled SO for an answer that makes sense to my question, so don't hate me if this is an easy one!
I am adding two views to the window:
self.appView = [[AppViewController alloc] initWithNibName:nil bundle:nil] ;
self.buttonBar = [[ButtonBar alloc] initWithNibName:nil bundle:nil];
[window insertSubview:[self.appView view] belowSubview:[self.launchScreen view]];
[window insertSubview:[self.buttonBar view] belowSubview:[self.launchScreen view]];
Before I remove the self.launchScreen.
When I add elements to the appView (it's a uitableviewcontroller) they work as expected, but when I add a button to the buttonBar (either in the XIB or via code), click them causes EXC_BAD_开发者_开发问答ACCESS.
Here's the init code from buttonBar (which is adding one button):
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.settingsButton = [[UIButton alloc] initWithFrame:CGRectMake(0,10,40,28)];
[self.settingsButton addTarget:self action:@selector(settings:) forControlEvents:UIControlEventTouchUpInside];
UIImage *btnImage = [UIImage imageNamed:@"play.png"];
[self.settingsButton setImage:btnImage forState:UIControlStateNormal];
[btnImage release];
[self.view addSubview:self.settingsButton];
}
return self;
}
My question is in two parts, 1) WHAT GIVES?! I've been at this way too long, and 2) How can I debug this stuff? I hate coming to SO to ask n00b questions when I'm sure XCODE's debugging tools would help me track this stuff down...
Use @selector(settings)
instead.
I'm not exactly sure if this is the problem but I dont think you need the "retain" at the end in this line
self.settingsButton = [[[UIButton alloc] initWithFrame:CGRectMake(0,10,40,28)] retain];
I'm assuming settingsButton is already in your header file with a @property(retain) and you have synthesized it in your .m file.
release it in the dealloc method instead.
精彩评论