Swift protocol and delegate (proxy), swift delegate
Protocol (Protocols)
It is used to unify the names of methods and properties, without implementing any function. It can be implemented by classes, enumerations, and struct. All the items that meet the Protocol requirements become the Protocol owner, the owner must provide the members specified by the Protocol, such as methods, attributes, operators, and subscripts.
Syntax: protocol SomeProtocol {// protocol definition goes here} // implement the protocol by colons in the middle. If there are multiple protocols, the protocols are separated by commas (,) struct SomeStructure: FirstProtocol, anotherProtocol {// structure definition goes here} // simultaneous implementation protocol containing the parent class. The parent class is written before the Protocol class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol {// class definition goes here}
Delegate Mode
A delegate is a design pattern that allows classes or struct to assign some functions that require their responsibilityDelegated)
To other types.
The implementation of the Delegation mode is simple: DefinitionProtocol
ComeEncapsulation
Those that need to be delegatedFunctions and Methods
To make itFollowers
Has these delegated functionsAnd Methods
.
The delegate mode can be used to respond to specific actions or receive data from external data sources without the need to know the types of external data sources.
Syntax
Code of FirstViewController
Class FirstViewController: UIViewController, SecondViewControllerDelegate {@ IBOutlet weak var showDelegateTextLabel: UILabel! Override func viewDidLoad () {super. viewDidLoad () // Do any additional setup after loading the view .} // click the button to jump to SecondViewController @ IBAction func tapGoSecondViewController (sender: UIButton) {// load SecondViewController let secondVC = UIStoryboard (name: "Main", bundle: NSBundle. mainBundle ()). instantiateViewControllerWithIdentifier ("secondViewController")! SecondViewController secondVC. delegate = self // jump to SecondViewController self. navigationController ?. PushViewController (secondVC, animated: true)} // MARK:-SecondViewControllerDelegate func fetchBackString (str: String) {self. showDelegateTextLabel. text = str} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .}}
Code of SecondViewController
Import UIKitprotocol SecondViewControllerDelegate: NSObjectProtocol {func fetchBackString (str: String)} class SecondViewController: UIViewController {@ IBOutlet weak var inputTextField: UITextField! Weak var delegate: SecondViewControllerDelegate? Override func viewDidLoad () {super. viewDidLoad () // Do any additional setup after loading the view.} @ IBAction func delegateBackMethod (sender: UIButton) {if self. delegate! = Nil {if let tempString = self. inputTextField. text {delegate !. FetchBackString ("proxy returned data: \ (tempString)")} self. navigationController ?. PopViewControllerAnimated (true)} override func didReceiveMemoryWarning () {super. didreceivemorywarning () // Dispose of any resources that can be recreated .}}