我有一个自定义的UIView子类,该子类附加了一些UIButtons。对于每个按钮,在UIViewController上定义了目标动作,其中包括自定义UIView。
以某种方式<< TouchUpInside事件不会被捕获在包括ViewController在内的自定义视图中。由于UIViews是UIResponderChain的一部分,所以我想知道为什么不触发事件吗?委派目标动作是否有意义,还是应该以其他方式完成?
Custom UIView子类:CustomView
let button = UIButton(frame: CGRectMake(10.0, 10.0, 100.0, 100.0))
button.addTarget(self.delegate, action: "buttonTapped:", forControlEvents: .TouchUpInside)
ViewController
// Include the custom view
let customView = CustomView()
customView.delegate = self
self.view.addSubview(customView)
...
func buttonTapped(sender: AnyObject!) {
// Doesn't get called
}
userInteractionEnabled
设置为YES
?在不是从NO
降级的视图上(默认是按钮),默认情况下为UIResponder
。如果视图为userInteractionEnabled = NO
,它将忽略触摸事件。