UIButton没有回应接触

时间:2016-08-09 03:44:22

标签: ios iphone swift uiviewcontroller uibutton

我有以下观点:

enter image description here

它的构建方式

UIViewController - > UIView(底部方块) - > UIViewController(加载在底部正方形)

未触发此UIViewController中的按钮按下。但是,当我在模拟器中按下时,我确实看到了按钮的动画效果。

此外,当我以编程方式将以下tap处理程序添加到此UIViewController中的圆形UIImageView时,它会响应:

func handleTap(sender: UITapGestureRecognizer? = nil) {
        print("tappable")
    }

let tap = UITapGestureRecognizer(target: self, action: #selector(NeighbourhoodViewController.handleTap(_:)))
userPopupView.userImageView.userInteractionEnabled = true
userPopupView.userImageView.addGestureRecognizer(tap)

如何捕捉这些按钮的动作?这不起作用:

@IBAction func test(sender: AnyObject) {
        print("test")
    }

该行已连接到界面构建器btw中的UIButton:

enter image description here

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。我只是将ViewController添加到View:

self.popupView.addSubview(userPopupView.view)

应该是:

self.addChildViewController(userPopupView)
self.popupView.addSubview(userPopupView.view)
userPopupView.didMoveToParentViewController(self)

现在有效!