简体   繁体   中英

iOS Custom .xib view button tap detect in view controller (Swift)

So I am using a custom View (.xib) which contains couple of objects: UIImage view & UIButton . The custom view in contained and used in View controller UIView , however I can't seem to figure out how to detect if the button(from .xib) is tapped and launch a function in the view controller. Any suggestions?

I can However add UITap gesture to the entire Viewcontroller UIView, however Thats not what i need.

在此处输入图片说明

@IBAction func calculateTapped(sender : AnyObject) {

     //You can get action here
}

Step 1 Link Your all button to to xib cocoa class

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

step 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)           
    }

step 3

Create Function for Button that can handle your click event

 @objc func acceptBtnPressed (sender:UIButton) { print("Button Pressed") }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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