UIButton在与另一个UIButton重叠时没有响应

时间:2012-09-05 11:22:24

标签: iphone uikit uibutton

我有UIViewController名为MyViewController。 MyViewController的视图有一个名为UIButton的{​​{1}}作为子视图。然后MyViewController的视图添加另一个名为MyFirstButton的子视图。 MySubView直接放在MySubView上方,因此看不到MyFirstButtonMyFirstButton依次将MySubView称为UIButton作为子视图。

问题是 - 如果MySecondButton的框架与MyFirstButton的框架重叠,则MySecondButton不响应。

我尝试通过将MySecondButton设置为NO来设置MyViewController的视图并将YES设置为userInteractionEnabled来尝试修复它,但它没有帮助。该按钮仍无响应。如何启用位于另一个按钮上的按钮?

2 个答案:

答案 0 :(得分:0)

你无法对MySecondButton下的特定MyFirstButton使用该动作...我不认为这可以完成..你可以实际做的是隐藏MySecondButton点击它以显示MYFirstButton然后在MYFirstButton上点击它的动作执行并在第一个上面再次显示MYSecondButton :)同样用子视图隐藏2 ...

答案 1 :(得分:0)

试试这个,

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *firstButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [firstButton setTitle:@"first" forState:UIControlStateNormal];
    firstButton.frame=CGRectMake(40, 70, 80, 80);
    [firstButton addTarget:self action:@selector(firstButtonClicked) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:firstButton];

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    [firstButton addSubview:view];
    UIButton *secondButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [secondButton setTitle:@"second" forState:UIControlStateNormal];
     secondButton.frame=CGRectMake(0, 0, 80, 80);
    [secondButton addTarget:self action:@selector(secondButtonClicked) forControlEvents: UIControlEventTouchUpInside];
    [view addSubview:secondButton];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)firstButtonClicked {
    NSLog(@"first");
}
-(void)secondButtonClicked {
    NSLog(@"second");
}