简体   繁体   中英

Button click not working when in presence of single Tap gesture

I have view hierarchy as follows

MyViewController --> ScrollView(added via IB) --> ImageView (+ zooming there) ---> button added on image View (added via code)

imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map.png"]];
[scroll addSubview:imageView];

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

myButton.frame = CGRectMake(660, 120, 40, 40);

[myButton setTitle:@"10" forState:UIControlStateNormal];

[myButton addTarget:self action:@selector(asiaPressed:) forControlEvents:UIControlEventTouchUpInside];

[myButton setEnabled:YES];
[imageView addSubview:myButton];

later on in view controller, i defined selector as

-(void) asiaPressed:(id)sender{
NSLog(@"asiaPressed clicked");

}

But it is never going inside selector..

Pls help...i guess it is very basic error..

Thanks in advance

UIImageView has userInteractionEnabled property set to NO by default so it (and its subviews) does not receive touch events. You need to set that property to YES manually:

...
imageView.userInteractionEnabled = YES;
...

PS You also mention gesture recognizer in question title, however have not posted any code with it - so if it present it may also cause the problems, but the problem is most likely is what I described above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM