简体   繁体   English

轻击UIButton不会调用其动作,尽管在视觉上它会接收事件

[英]Tapping UIButton doesn't call its action although visually it receives the event

I have a view in a xib file that contains a button and other 2 views and they don't overlap. 我在xib文件中包含一个按钮和其他2个视图的视图,并且它们不重叠。 The view file's owner is a custom class derived from UIViewController. 视图文件的所有者是从UIViewController派生的自定义类。 The custom view controller is created programmatically by this code: 定制视图控制器是通过以下代码以编程方式创建的:

let sfvc = SelezioneFascicoloViewController(nibName:  "SelezioneFascicoloView", bundle: nil)
viewObject.addSubview(sfvc.view)
sfvc.updateWithAttributes(attrs, inFascicolo: self.fascicolo!)

viewObject is a view contained in a custom UICollectionViewCell. viewObject是包含在自定义UICollectionViewCell中的视图。

The button has a handler connected to the touchDown event (but any other event produce the same problem) and it is defined in the custom UIViewController (SelezioneFascicoloViewController) I have flagged showsTouchOnHighlight for the button just to see if it receive the event. 该按钮具有一个与touchDown事件连接的处理程序(但任何其他事件都会产生相同的问题),并且它是在自定义UIViewController(SelezioneFascicoloViewController)中定义的,我已将按钮标记为showTouchOnHighlight,以查看它是否接收到该事件。

Well, the view shows up correctly, if I tap the button I can see the glow but the handler isn't called. 好吧,该视图正确显示,如果我点击按钮,我可以看到辉光,但未调用处理程序。 All the views in the hierarchy have the UserInteractionEnabled set to true, and their sizes are correct so the button isn't outside the superview frame. 层次结构中的所有视图都将UserInteractionEnabled设置为true,并且它们的大小正确,因此按钮不在Superview框架之外。

Also I have a UITapGestureRecognizer in the UICollectionViewController derived class that contains the UICollectionViewCell, and its handler is fired if I tap outside the button, but not when I tap inside the button, and this is as it should be. 另外,我在包含UICollectionViewCell的UICollectionViewController派生类中有一个UITapGestureRecognizer,如果我在按钮外部点击,则触发其处理程序,但在按钮内部点击时不触发它的处理程序,这是应该的。 Can someone give me some advice for this kind of problem? 有人可以给我一些有关此类问题的建议吗?

Edit: added a screenshot 编辑:添加了屏幕截图

Interface Builder screen shot Interface Builder屏幕截图

Thank beyowulf. 感谢beyowulf。 Everything works if I add SelezioneFascicoloViewController as a child view controller, in this way: 如果我通过以下方式将SelezioneFascicoloViewController添加为子视图控制器,则一切正常:

let sfvc = SelezioneFascicoloViewController(nibName: "SelezioneFascicoloView", bundle: nil)
self.addChildViewController(sfvc)
viewObject.addSubview(sfvc.view)
sfvc.didMoveToParentViewController(self)
sfvc.updateWithAttributes(attrs, inFascicolo: self.fascicolo!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM