0

I have a view in a xib file that contains a button and other 2 views and they don't overlap. The view file's owner is a custom class derived from 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.

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.

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.

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. Can someone give me some advice for this kind of problem?

Edit: added a screenshot

Interface Builder screen shot

Davide
  • 11
  • 4
  • Is there a filled in gray dot next to the IBAction of the button? – beyowulf Nov 25 '15 at 18:42
  • Yes, the handler is connected – Davide Nov 26 '15 at 16:24
  • Are there lots of other views in viewObject? What is the benefit of having a SelezioneFascicoloViewController? In general it is best to avoid adding uiviewcontroller views as sub views of other view controllers, but if you insist on doing it you should see the answers here: http://stackoverflow.com/questions/1486832/how-to-add-an-uiviewcontrollers-view-as-subview – beyowulf Nov 26 '15 at 23:22

1 Answers1

0

Thank beyowulf. Everything works if I add SelezioneFascicoloViewController as a child view controller, in this way:

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