1. 程式人生 > >iOS中按鈕不響應點選事件

iOS中按鈕不響應點選事件

今天寫iOS專案時,發現雖然為按鈕添加了點選事件,但是點選後卻無法響應,搜尋後才解決了問題:按鈕的所在的父控制元件如果不能互動的話,那麼這個按鈕也無法互動,當然,可以將父控制元件的userInteractionEnabled屬性設定為YES

我這個專案所在的按鈕是新增在UIImageView上的,所以按鈕無法進行互動。解決辦法:

imageView.userInteractionEnabled=YES;
            UIButton* start=[[UIButton alloc] init];
            start.frame=CGRectMake(kWidth/2
-50, kHeight-200, 100, 40);
[imageView addSubview:start]; [start addTarget:self action:@selector(startGame) forControlEvents:UIControlEventTouchUpInside];

這樣,問題就解決了。