2012-01-05 29 views
2

我有兩個UIButtons我創造編程和設定目標。如果我將這些按鈕添加到主視圖,則一切都很精美。然而,當我第一次加入到子視圖和這個子視圖添加到主視圖,觸摸事件不工作。我猜這些按鈕正在被掩蓋。addTarget不會在iOS的UIButtons工作,鑑於掩蓋了

我怎樣才能發現這些按鈕,同時保持它們的子視圖裏面?

這裏是我的代碼:

// Create button panel here 
UIImage *buttonPanel = [UIImage imageNamed:@"left_button_background"]; 
UIImageView *buttonPanelView = [[UIImageView alloc] initWithImage:buttonPanel]; 
[buttonPanelView setFrame:CGRectMake(20, 186, 363, 345)]; 

// Populate the panel with buttons 
UIButton *cmsButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 351, 142)]; 
[cmsButton setImage:[UIImage imageNamed:@"BTN_MATLIB"] forState:UIControlStateNormal]; 
[cmsButton addTarget:self action:@selector(loadCMS) forControlEvents:UIControlEventTouchDown]; 
[buttonPanelView addSubview:cmsButton]; 
[self setCms:cmsButton]; 

UIButton *calcButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 196, 351, 142)]; 
[calcButton setImage:[UIImage imageNamed:@"BTN_MORE_RESOURCES"] forState:UIControlStateNormal]; 
[calcButton addTarget:self action:@selector(loadCalc) forControlEvents:UIControlEventTouchDown]; 
[buttonPanelView addSubview:calcButton]; 
[self setCalc:calc]; 

[[self view] addSubview:buttonPanelView]; 

回答

12

圖像視圖沒有在默認情況下,這意味着他們沒有子視圖中可以接收用戶的交互使用戶交互。

buttonPanelView.userInteractionEnabled = YES;