Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

292

macrumors newbie
Original poster
Mar 22, 2009
2
0
I'm getting the dreaded EXC_BAD_ACCESS error when I'm trying to set an
image on a UIButton.

I read the Apple doc on memory management and still can't figure out
what I'm doing wrong. I've verified my images are all accessible and working in my Resources folder.

I've narrowed it down to the exact line of code that's causing the
error. If you can help me figure out what I'm doing wrong and how to fix
it, I'll be eternally grateful

I have a very simplified version of my code:
Here's my Header File:

MyController.h
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h> 

@interface MyController : UIViewController {
	IBOutlet UIButton *button1;
}

@property (nonatomic, retain) UIButton *button1;

- (IBAction)clickedAButton:(id)sender;
- (NSString *)doSomething:(int)someNumber;

@end

And here's my implementation file. I noted in the comments below which line causes the error:
Code:
#import "MyController.h"

@implementation MyController
@synthesize button1;

- (IBAction)clickedAButton:(id)sender{
   NSString *myString;
   [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//This line works perfectly
   [sender setImage:[UIImage imageNamed:@"Red.png"] forState:UIControlStateNormal]; //This line works Perfectly
     
   myString = [self doSomething:1];   
}

- (NSString *)doSomething:(int)someNumber {

   [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];//This line works perfectly
   [button1 setImage:[UIImage imageNamed:@"Blue.png"] forState:UIControlStateNormal];//This line causes the EXC_BAD_ACCESS Error

return @"didSomething";
}
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
I don't see anything wrong with what you've posted. I assume you must be doing something else interesting between setting the color and background to one thing and then setting it back to another.

My guess is some of the code you didn't post is actually where the bug is.
 

bboyjayz

macrumors member
Mar 20, 2009
55
0
that's true, i don't see mistake on this code me too ... i already had this error, that's was a problem of cache when i changed subviews of the windows.... mayby it will help you ...
 

buckyballs

macrumors regular
Dec 22, 2006
176
97
You also have this problem if you use NSArrays objectAtIndex method and try to access and index that doesn't exist.
 

292

macrumors newbie
Original poster
Mar 22, 2009
2
0
Thank you everybody for your responses.

eddietr, you are correct. I cut out all the extraneous code to focus on the bare minimum amount of code to focus on the problem. When I ran this code, the app worked. I'm going to paste back in the rest of the code line by line until I figure out which line is causing the issue.

Thank you all again... I may be back...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.