Skip to content

Instantly share code, notes, and snippets.

@charlesferreira
Last active February 23, 2018 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlesferreira/84040ad03a0eb78fa4db4046468c55e4 to your computer and use it in GitHub Desktop.
Save charlesferreira/84040ad03a0eb78fa4db4046468c55e4 to your computer and use it in GitHub Desktop.
Simple custom view delegate example
import UIKit
protocol CustomViewDelegate {
func customView(_ customView: CustomView, tappedButton button: UIButton)
}
class CustomView: UIView {
weak var delegate: CustomViewDelegate?
// <control-drag> from .XIB's button to here
@IBAction func buttonTapped(_ sender: Any) {
guard let button = sender as? UIButton else { return }
delegate?.customView(self, tappedButton: button)
}
// ...
}
import UIKit
class ViewController: UIViewController {
// <control-drag> from storyboard to here
@IBOutlet weak var customView: CustomView!
override func viewDidLoad() {
super.viewDidLoad()
customView.delegate = self
}
// ...
}
extension ViewController: CustomViewDelegate {
func customView(_ customView: CustomView, tappedButton button: UIButton) {
print("Button in custom view was tapped")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment