Swift language implements proxy value/closure pass value

Source: Internet
Author: User
Tags uikit

1. Requirements: Use the proxy to achieve the reverse value (example using the button in the second view controller to change the contents of the first View Controller label)

One, the first interface

1 classViewcontroller:uiviewcontroller, changetestlabeldelegate {2var testlabel:uilabel?3     Overridefunc viewdidload () {4 super.viewdidload ()5         //additional setup after loading the view, typically from a nib.6Self.title ="Swift Proxy Transfer value";7Let Rightbuttonitem:uibarbuttonitem = Uibarbuttonitem (title:"Next Page", style:. Plain, target:self, action:"NextPage");8Self.navigationItem.rightBarButtonItem =Rightbuttonitem;9         TenTestLabel = UILabel (Frame:cgrectmake (0, $, the, -)); OneTestLabel?. Layer.bordercolor =Uicolor.redcolor (). Cgcolor; ATestLabel?. Layer.borderwidth =1; -TestLabel?. Layer.cornerradius =5; -TestLabel?. Text ="Learn Swift"; theTestLabel?. TextAlignment =Nstextalignment.center; -Self.view.addSubview (testlabel!); -     } -  + func nextPage () { -Let Secondvc:secondviewcontroller =Secondviewcontroller (); +SECONDVC.Delegate=Self ; ASelf.navigationcontroller?. Pushviewcontroller (SECONDVC, animated:true); at     } -      -Func Changelabeltext (Controller:secondviewcontroller,string: String) { -TestLabel?. Text =string; -println"Testlabel.text = = \ (string)"); -     } in     Overridefunc didreceivememorywarning () { - super.didreceivememorywarning () to         //Dispose of any resources the can be recreated. +     } -  the  *}

Two, the second interface

1 Import Foundation2 Import UIKit3 4 //define the protocol to change the label content5 protocol Changetestlabeldelegate:nsobjectprotocol {6     //callback Method7Func Changelabeltext (Controller:secondviewcontroller,string: String);8 }9 Ten classSecondviewcontroller:uiviewcontroller { Onevar temp =0; AVarDelegate: Changetestlabeldelegate? -     Overridefunc viewdidload () { - super.viewdidload () theSelf.title ="Secondviewcontroller"; -Self.view.backgroundColor =Uicolor.yellowcolor (); -Let rect = CGRect (x: -Y: $, Width: Max, Height: -); -var MyButton =UIButton (frame:rect); +Mybutton.center = Cgpointmake ( the, $); -Mybutton.settitle ("Change the label content", Forstate:. Normal); + Mybutton.settitlecolor (Uicolor.redcolor (), forState:UIControlState.Normal); AMybutton.addtarget (self,action:"btnclicked", forControlEvents:. Touchupinside); at Self.view.addSubview (MyButton); -          -     } - func btnclicked () { -temp++; -println"I was clicked on \ (temp) time.") in         if(Delegate!=Nil) { -             Delegate?. Changelabeltext (Self,string:"Learn Swift"+"\ (temp)"); to         }     +     } -}

2. Requirements: Using closures to achieve reverse value transfer

1 Import UIKit2 3 classViewcontroller:uiviewcontroller {4var testlabel:uilabel?5     Overridefunc viewdidload () {6 super.viewdidload ()7         //additional setup after loading the view, typically from a nib.8Self.title ="Swift Closure Pass value";9Let Rightbuttonitem:uibarbuttonitem = Uibarbuttonitem (title:"Next Page", style:. Plain, target:self, action:"NextPage");TenSelf.navigationItem.rightBarButtonItem =Rightbuttonitem; One          ATestLabel = UILabel (Frame:cgrectmake (0, $, the, -)); -TestLabel?. Layer.bordercolor =Uicolor.redcolor (). Cgcolor; -TestLabel?. Layer.borderwidth =1; theTestLabel?. Layer.cornerradius =5; -TestLabel?. Text ="Swift Closures"; -TestLabel?. TextAlignment =Nstextalignment.center; -Self.view.addSubview (testlabel!); +     } -  + func nextPage () { ALet Secondvc:secondviewcontroller =Secondviewcontroller (); at secondvc.initwithclosure (somefunctionthattakesaclosure); -Self.navigationcontroller?. Pushviewcontroller (SECONDVC, animated:true); -     } -Func Somefunctionthattakesaclosure (string: String),Void { -testlabel!. Text =string -     } in     Overridefunc didreceivememorywarning () { - super.didreceivememorywarning () to         //Dispose of any resources the can be recreated. +     } -  the  *}
1 Import Foundation2 Import UIKit3 //similar to the typedef in OC4Typealias changelabeltextclosure = (string: String),Void;5 6 classSecondviewcontroller:uiviewcontroller {7var myclosure:changelabeltextclosure?//declaring a closed packet8var temp =0;9Func initwithclosure (closure:changelabeltextclosure?) {TenMyclosure = closure;//assign a function pointer to the Myclosure closure, which covers references to local variables in the Somefunctionthattakesaclosure function, etc. One     } A     Overridefunc viewdidload () { - super.viewdidload () -Self.title ="Secondviewcontroller"; theSelf.view.backgroundColor =Uicolor.yellowcolor (); -Let rect = CGRect (x: -Y: $, Width: Max, Height: -); -var MyButton =UIButton (frame:rect); -Mybutton.center = Cgpointmake ( the, $); +Mybutton.settitle ("Change the label content", Forstate:. Normal); - Mybutton.settitlecolor (Uicolor.redcolor (), forState:UIControlState.Normal); +Mybutton.addtarget (self,action:"btnclicked", forControlEvents:. Touchupinside); A Self.view.addSubview (MyButton); at          -     } - func btnclicked () { -temp++; -println"I was clicked on \ (temp) time.") -             if(Myclosure! =Nil) { in                 //closed Baoyin call somefunctionthattakesaclosure function: callback.  -myclosure! (string:"closure pass value \ (temp)"); to             } +     } -}

Swift language implements proxy value/closure pass 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.