addTarget不适用于iOS中的UIButtons,视图被遮盖

时间:2012-01-05 05:40:29

标签: ios cocoa-touch uiview uibutton touch

我有两个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];

1 个答案:

答案 0 :(得分:12)

默认情况下,图片视图未启用用户互动,这意味着他们的任何子视图都无法接收用户互动。

buttonPanelView.userInteractionEnabled = YES;