views:

38

answers:

2

Here is the code I'm using:

-(void)viewDidLoad
{
    NSString *checkerPath = [[NSBundle mainBundle] pathForResource:@"black-yellow-checker" ofType:@"png"];
    UIImage *checkerImage = [[UIImage alloc] initWithContentsOfFile:checkerPath];
    checkerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 230.0f, 320.0f, 280.0f)];
    [checkerView setImage:checkerImage];

    UIButton *backButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    backButton.frame = CGRectMake(45.0f, 175.0f, 230.0f, 50.0f);
    [backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yellowButton" ofType:@"png"]] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(goBackHome:) forControlEvents:UIControlEventTouchDown];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont fontWithName:@"Marker Felt" size:30];
    backButton.titleLabel.textColor = [UIColor blackColor];
    [checkerView addSubview:backButton];
}

- (void)goBackHome:(id)sender
{
    NSLog(@"go back pressed");
}

Not sure why this doesn't work. The button shows up, but when I press it nothing happens. It doesn't even change to the "indented" image when I touch it. Nothing. Can't use IB, has to be programmatically. Any thoughts?

A: 

UIImageView has userInteractionEnabled set to NO by default. This prevents any subviews from getting touches.

You could either enable user interaction of the image view by

checkerView.userInteractionEnabled = YES;

or create a new UIView to include both checkerView and the backButton.

UIView* newView = ...
...
[newView addSubview:checkerView];
[newView addSubview:backButton];
KennyTM
AHA. thought it would be yes by default. since you're so darn knowledgable you should give this a try http://stackoverflow.com/questions/3808087/where-to-find-what-mkdomainerror-error-4-means
marty
+3  A: 

I believe that the forControlEvents: has to be UIControlEventTouchUpInside.

Craig Howarth
nah it works with uicontroleventouchdown too
marty