iOS自定义.xib视图按钮点击检测视图控制器(Swift)

时间:2015-08-22 05:51:38

标签: ios swift uiview uikit xib

所以我使用的是包含几个对象的自定义视图(.xib):UIImage view& UIButton。视图控制器UIView中包含和使用的自定义视图,但我似乎无法弄清楚如何检测按钮(来自.xib)是否被点击并在视图控制器中启动一个功能。有什么建议?

我可以将UITap手势添加到整个Viewcontroller UIView,但这不是我需要的。

2 个答案:

答案 0 :(得分:0)

enter image description here

@IBAction func calculateTapped(sender : AnyObject) {

     //You can get action here
}

答案 1 :(得分:0)

步骤1     将您的全部按钮链接到xib可可类

class DialogHandler: UIView { @IBOutlet weak var acceptBtn: UIButton! @IBOutlet weak var closeBtn: UIButton! }

第2步

  Add Below code in viewDidLoad() 

if let customView = Bundle.main.loadNibNamed("DialogDemo", owner: self, options: nil)?.first as? DialogHandler {
        dialogView = customView
        customView.acceptBtn.addTarget(self, action: #selector(ViewController.acceptBtnPressed(sender:)), for: .touchUpInside)           
    }

第3步

为Button创建可以处理您的点击事件的功能

 @objc func acceptBtnPressed (sender:UIButton) { print("Button Pressed") }
相关问题