Unable to Get UIButton Software to Work - iPhone

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");
}

I don’t know why this is not working. A button appears, but when I click, nothing happens. When I touch it, it doesn't even change to indent. Nothing. You can not use IB, it must be software. Any thoughts?

+5
source share
2 answers

UIImageView has userInteractionEnabledinstalled by default by default . This prevents any subzones from entering.

You can either enable user interaction with the image view using

checkerView.userInteractionEnabled = YES;

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

UIView* newView = ...
...
[newView addSubview:checkerView];
[newView addSubview:backButton];
+6

, forControlEvents: UIControlEventTouchUpInside.

+3

All Articles