I am new in Swift. I'm trying to understand how the delegate pattern works. Below is some code that I have tried to implement. The goal is to execute one method (printing()) from another class. I cannot achieve the expected behaviour for some reasons.
The first class:
protocol ViewControllerDelegate: class {
func printing()
}
class ViewController: UIViewController {
weak var delegate: ViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func button(_ sender: Any) {
delegate?.printing()
}
}
The second class:
class Class: UIViewController,ViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let vc = ViewController()
vc.delegate = self
}
func printing () {
print("PRINT")
}
}