Rick_CBR929RR
Rick_CBR929RR

Reputation: 307

UIButton added in code not responding to touch

I'm just adding a quick intro screen with a button, which compiles and displays properly, but my button doesn't respond.

- (void)viewDidLoad {
   [super viewDidLoad];
   NSLog(@"viewDidLoad!");
   [self showIntro];
}

//  make a quick Intro screen
- (void) showIntro {
   UIImageView *myBackImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
   [myBackImage setImage:(UIImage *) [UIImage imageNamed:@"myBkgnd.png"]];

   UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom ];
   [clickBtn addTarget:self action:@selector(myButton_pressed:) forControlEvents:UIControlEventTouchUpInside];
   [clickBtn setImage:(UIImage *) [UIImage imageNamed:@"btnImage.png"] forState:UIControlStateNormal];
   clickBtn.frame = CGRectMake(111, 200, 75, 60);
   [myBackImage addSubview:clickBtn];
   [self.view addSubview:myBackImage];
}

- (void)myButton_pressed:(UIButton*)button {
   NSLog(@"Button Pressed!");
}

What am I overlooking? Thanks!

Upvotes: 1

Views: 1070

Answers (1)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

As you have added UIButton in UIImageView but didn't you forgot imageView's userInteraction by default NO. So :

myBackImage.userInteractionEnabled = YES;

EDIT : Just formatted

Upvotes: 3

Related Questions