Swift protocol and delegate (proxy), swift delegate

Source: Internet
Author: User

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: DefinitionProtocolComeEncapsulationThose that need to be delegatedFunctions and MethodsTo make itFollowersHas 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 .}}

 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.