Swift uses proxies and closures (closure) to reverse the value

Source: Internet
Author: User
Tags closure uikit

Firstviewcontroller's Code

Import UIKitclassFirstviewcontroller:uiviewcontroller, secondviewcontrollerdelegate {@IBOutlet weak var showtextlabel:uilabel!@IBOutlet weak var showdelegatetextlabel:uilabel!Overridefunc 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 from storyboardLet SECONDVC = Uistoryboard (name:"Main", Bundle:NSBundle.mainBundle ()). Instantiateviewcontrollerwithidentifier ("Secondviewcontroller") as!Secondviewcontroller//implement callback to get back value of callback (closure) Secondvc.backclosure ={(backstr:string)-VoidinchSelf.showTextLabel.text=backstr} SECONDVC.Delegate= Self//Jump to SecondviewcontrollerSelf.navigationcontroller?. Pushviewcontroller (SECONDVC, animated:true) } //MARK:-secondviewcontrollerdelegate(agent) func fetchbackstring (str:string) {Self.showDelegateTextLabel.text=STR}Overridefunc didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated. }}
SecondviewcontrollerThe Code
Import uikit//defines the closure type (Specific function type function type) Typealias Inputclosuretype = (String), Voidprotocol secondviewcontrollerdelegate:nsobjectprotocol{func fetchbackstring (str:string)}class SecondViewController:    Uiviewcontroller {@IBOutlet weak var inputtextfield:uitextfield!    Receive the last page sent over the closure block Var backclosure:inputclosuretype?        Weak var delegate:secondviewcontrollerdelegate?    Override Func Viewdidload () {super.viewdidload ()//Do any additional setup after loading the view. } @IBAction func Tapbackbutton (Sender:uibutton) {if self.backclosure! = nil {if let tempstring = S Elf.inputTextField.text {self.backclosure! ( tempstring)}} Self.navigationcontroller?. Popviewcontrolleranimated (True)} @IBAction func Delegatebackmethod (Sender:uibutton) {if self.delegate ! = Nil {if let tempstring = self.inputTextField.text {delegate!. Fetchbackstring ("Proxy return data: \ (tempstring)}} Self.navigationcontroller?. Popviewcontrolleranimated (True)} override Func didreceivememorywarning () {super.didreceivememorywarning    ()//Dispose of any of the resources that can be recreated. }}
Effect View:

Swift uses proxies and closures (closure) to reverse the value

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.