Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton causing crashes

This is a very simple issue but I have no idea why it's happening. In my header file, I've declared a UIButton *leftButton. In the viewDidLoad method I do this leftButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];

Then in some other method I have:

    leftButton.frame=newFrame;
    leftButton.tag=i;
    [leftButton addTarget:self action:@selector(leftButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];

However I keep getting a crash when the above method is called. Sometimes I get a message, other times it just crashes with no message. I can't really replicate any crash messages, now all I'm getting is just a general crash, but it kept crashing from weird issues such as [UIGestureRecognizer setFrame] is causing problems or [WebView setFrame] or something..for some reason it keeps thinking that leftButton is something else than a UIButton, but this is not the case. There is nothing else named leftButton in my entire project. Any ideas?

like image 397
Snowman Avatar asked Jul 16 '26 10:07

Snowman


1 Answers

Your first method is returning an autoreleased object, most likely it has been released by the time your second method is called. Either retain the button in the first method or use a retained property to hold on to it rather than an ivar.

Also, remember to release this in your dealloc method at the end.

like image 138
jrturton Avatar answered Jul 17 '26 23:07

jrturton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!