Last active
February 23, 2018 18:15
-
-
Save charlesferreira/84040ad03a0eb78fa4db4046468c55e4 to your computer and use it in GitHub Desktop.
Simple custom view delegate example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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