NinjaLikesCheez
NinjaLikesCheez

Reputation: 454

UIButton crashing app on touch

I have a navigationController which holds a view with some buttons, but when I press a button I get a EXC_BAD_ACCESS error. I can't think what I'm doing wrong as the target is set right. It crashes whether the the button is added programmatically or via IB.

Button Code:

UIButton *reportsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
reportsButton.frame = CGRectMake(20, 100, 100, 50);
[reportsButton setTitle:@"Reports" forState:UIControlStateNormal];
[reportsButton addTarget:self action:@selector(reportsButtonTouched:) forControlEvents:UIControlEventTouchUpInside];

Function button is trying to access:

- (void)reportsButtonTouched:(id)sender
{
    NSLog(@"working");
}

Error:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); //EXC_BAD_ACCESS code=1
}

The function the button is trying to access exists.

Maybe it's something about the way the NavigationController functions that I'm not aware of, but I've done this before without any problems.

Thanks for any answers, I really do appreciate the help I've got from this site before.

EDIT: This is my AppDelegates didFinishLaunching incase that helps in any way.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *homevc = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:homevc];

    [self.window addSubview:nav.view];
    [self.window makeKeyAndVisible];
    return YES;
}

Upvotes: 2

Views: 850

Answers (2)

Nolan Anderson
Nolan Anderson

Reputation: 558

I was also getting this error; I had been programming using SpriteKit and implementing my buttons a certain way, and then when I went back to not using the framework, all of a sudden every button I implemented was causing a bad access error.

I found out that I was initializing my buttons in viewDidLoad, because I had been using didMoveToView, and I accidentally treated them as the same function (which to be honest I don't really have a firm grasp on what viewDidLoad actually does).

But when I did this:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
    // Custom initialization
    [self addButton];

}
return self;
}


- (void)addButton
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];

[button setTitle:@"CLICK" forState:UIControlStateNormal];


[button addTarget:self action:@selector(buttonPressed:) 
forControlEvents:UIControlEventTouchDown];

[self.view addSubview:button];
}

- (void)buttonPressed:(UIButton *)button
{
    NSLog(@"WORKED"); 
}

Everything worked out fine... So try that, if you're still having trouble. Good luck!

Upvotes: 1

WrightsCS
WrightsCS

Reputation: 50697

Nothing in your code seems to point to any issues. Use this tutorial to make Xcode break on all exceptions. This will take you closer to the 'scene of the crime' rather than crash to main.

Upvotes: 4

Related Questions