[iOS] [Swift] Viewcontroller reverse value using Swift closure

Source: Internet
Author: User
Tags uikit

Closure Reference: http://c.biancheng.net/cpp/html/2285.html closure detailed

Value reference: Http://www.tuicool.com/articles/vy2uUz

Swift uses closures (closure) to transmit values to and from two controllers

Import UIKitclassZwrootviewcontroller:uiviewcontroller {init (nibname nibnameornil:string?, bundle Nibbundleornil:nsbundle?) {super.init (Nibname:nibnameornil, Bundle:nibbundleornil)//Custom Initialization} var Mylabel:uilabel?Overridefunc viewdidload () {super.viewdidload () var item= Uibarbuttonitem (title:"Next Page", Style:uibarbuttonitemstyle.plain,target:self,action:"nextbtnclicked") Self.navigationItem.rightBarButtonItem=Item MyLabel= UILabel (Frame:cgrectmake (0, -, the, -)) MyLabel!. Text ="Closure"MyLabel!. TextAlignment =nstextalignment.center Self.view.addSubview (MyLabel!)    //Do any additional setup after loading the view.} func somefunctionthattakesaclosure (string: String),Void {//function body goes heremylabel!. Text =string} func nextbtnclicked () {Let second=Zwsecondviewcontroller (Nibname:nil,bundle:nil)//The current somefunctionthattakesaclosure function pointer is passed to the second interface, and the second interface's closure gets the function pointer and then callbacks the functionsecond.initwithclosure (somefunctionthattakesaclosure) Self.navigationController.pushViewController (second, Animated:true)      }    Overridefunc viewwilldisappear (animated:bool) {MyLabel!. Hidden =true  }  Overridefunc viewwillappear (animated:bool) {MyLabel!. Hidden =false  }  Overridefunc didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated.  }    /*//#pragma mark-navigation//In a storyboard-based application, you'll often want to do a little preparation B Efore navigation override func Prepareforsegue (Segue:uistoryboardsegue?, Sender:anyobject?)    {//Get the new view controller using [Segue Destinationviewcontroller].  Pass the selected object to the new view controller. }  */}

Import uikit//is similar to Typedeftypealias sendvalueclosure= (string:string)->voidclass Zwsecondviewcontroller in OC:  Uiviewcontroller {var i:int?  Declare a closed-packet var myclosure:sendvalueclosure? The following method requires the Somefunctionthattakesaclosure function pointer passed into the previous interface, Func initwithclosure (closure:sendvalueclosure?). {//Assign the function pointer to the Myclosure closure, which covers references to local variables in the somefunctionthattakesaclosure function myclosure = closure} init (Nibname nibn Ameornil:string, bundle Nibbundleornil:nsbundle?) {Super.init (Nibname:nibnameornil, Bundle:nibbundleornil)//Custom initialization} override func Viewdidlo AD () {super.viewdidload () i = 0 var btn = uibutton.buttonwithtype (Uibuttontype.system) as? UIButton btn!. frame = CGRectMake (0,100,320,50) btn!. Settitle ("Click Me", ForState:UIControlState.Normal) btn!. AddTarget (self,action: "Action", ForControlEvents:UIControlEvents.TouchUpInside) Self.view.addSubview (BTN)//Do  Any additional setup after loading the view. } func action () {i = i!+1//Empty   If myclosure{//closed Baoyin call Somefunctionthattakesaclosure function: callback. myclosure!  (String: "Good oh \ (i)")}  } override func Didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be  Recreated. }/*//#pragma mark-navigation//In a storyboard-based application, you'll often want to do a little preparatio N before navigation override func Prepareforsegue (Segue:uistoryboardsegue?, Sender:anyobject?)    {//Get the new view controller using [Segue Destinationviewcontroller].  Pass the selected object to the new view controller. }  */}

  

[iOS] [Swift] Viewcontroller reverse value using Swift closure

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.