How to Create Delegate in Swift
·
Alexander Deplov
- In class that send the data create protocol:
protocol BackButtonActionDelegate {
func pressFinished(){}
}
- Inside this class:
class BackButton: SKNode {
var delegate: BackButtonActionDelegate?
}
- Inside the class send the data, run the delegate message when needed:
delegate?.pressFinished()
Inside another class, that received data, add delegate:
class OptionsWindow: SKNode, BackButtonActionDelegate {
}
Add same function from sender:
func pressFinished() {
print("delegate triggered")
}
Inside another class, that received data, in viewDidLoad() add:
backButton.delegate = self
Done.