Swift language implements proxy value

Source: Internet
Author: User

Requirements: Use the proxy to achieve the Echo value (the following example uses the button in the second view controller to change the contents of the label in the first view controller)

First, create Rootviewcontroller

Import Foundationimport Uikitclass rootviewcontroller:uiviewcontroller,changeworddelegate{var qzLabel:UILabel? Override Func Viewdidload () {super.viewdidload () Self.title = "Rootviewcontroller" Let Rightbtn:uibar Buttonitem = Uibarbuttonitem (title: "Next Page", Style:.) Plain,target:self,action: "NextPage") Self.navigationItem.rightBarButtonItem = rightbtn let rect = CG Rect (x:0,y:200,width:320,height:50) Qzlabel = UILabel (frame:rect) qzlabel!. Text = "Qin Zhiwei" qzlabel!.        TextAlignment = Nstextalignment.center Self.view.addSubview (qzlabel)} func nextPage () { Let svc = Secondviewcontroller () svc.delegate = self Self.navigationController.pushViewController (svc,anim ated:true)} func Changeword (controller:secondviewcontroller,string:string) {qzlabel!. Text = String println ("Qzlabel.text = = \ (String)")}}


Ii. creation of Secondviewcontroller

Import foundationimport uikit//Definition Protocol change label Content protocol changeworddelegate:nsobjectprotocol{//callback method func Changeword (Con troller:secondviewcontroller,string:string)}class secondviewcontroller:uiviewcontroller{var temp = 0 Var Delegate:C    Hangeworddelegate? Override Func Viewdidload () {super.viewdidload () Self.title = "Secondviewcontroller" self.view.backgr Oundcolor = Uicolor.greencolor () Let rect = CGRect (x:50,y:200,width:150,height:50) var MyButton = UIButton (f Rame:rect) Mybutton.center = Cgpointmake (160,200) mybutton.settitle ("Change label contents", Forstate:. Normal) Mybutton.addtarget (self,action: "btnclicked", forControlEvents:. Touchupinside) Self.view.addSubview (MyButton)} func btnclicked () {temp++ println ("I was clicked The ") if (delegate) {delegate?. Changeword (self,string: "Qin Zhiwei" +string (temp))}}}


Swift language implements proxy 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.