在UI中,基本上全部的控件的用戶交互都是打開的,可是也有一些特例,例如label、imageView。ide
userInteractionEnabled這個屬性影響了響應者鏈的檢測過程,因此若是將一個button放在一個label或者imageView上,都須要將label或者imageView的用戶交互打開才能夠,不然在檢測的過程當中是檢測不到label或者imageView上的子視圖的。spa
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; // 打開label的用戶交互 label.userInteractionEnabled = YES; [self.view addSubview:label]; UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)]; button.frame = CGRectMake(10, 10, 80, 30); [button setTitle:@"誓言の真噯" forState:(UIControlStateNormal)]; [button addTarget:self action:@selector(clickButton:) forControlEvents:(UIControlEventTouchUpInside)]; [label addSubview:button]; } - (void)clickButton:(UIButton *)button { NSLog(@"誓言的真噯"); }
若是label的用戶交互不打開,在視圖上能夠看到button,可是點擊以後沒有響應;因此要打開用戶交互。orm
一樣的還有imageView,默認交互也是NO,須要時記得打開。。。blog