userInteractionEnabled没有传播到子视图?

时间:2016-01-15 09:09:18

标签: ios objective-c

我有一个包含ContainerView : UIView的自定义UIButton。 我打开和关闭userInteractionEnabled的{​​{1}}属性以响应各种事件。有时,当容器视图的ContainerView为NO时,会触发tappedButton:方法。这怎么可能?

userInteractionEnabled

我可以通过向ContainerView添加以下方法来解决此问题:

@implementation ContainerView
-(void)addButton {
    self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(tappedButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"XX" forState:UIControlStateNormal];
    [self addSubview:button];
}

-(void)tappedButton:(id)sender {
    // Assertion sometimes fails! 
    // Especially when tapping multiple times very quickly
    assert(self.userInteractionEnabled); 
    // ... do other stuff...
}
// ...
@end

为什么这可以解决问题?我的理解是,设置// With this method, the assertion in the above code sample never fails -(void)setUserInteractionEnabled:(BOOL)userInteractionEnabled { self.button.userInteractionEnabled = userInteractionEnabled; [super setUserInteractionEnabled:userInteractionEnabled]; } 会阻止子视图接收触摸。

1 个答案:

答案 0 :(得分:0)

您还可以使用当前代码

设置按钮的enabled属性
 [button1 setEnabled:YES];
 button1.userInteractionEnabled = YES;