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

Source: Internet
Author: User

One, the first interface

Created by Qin Zhiwei on 14-6-13.import uikitclass Zwrootviewcontroller:uiviewcontroller {Init (nibname nibnameornil:string?, bundle Nibbundleornil:nsbundle?) {  Super.init (Nibname:nibnameornil, Bundle:nibbundleornil)  Custom initialization}var mylabel:uilabel?Override Func 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,100,320,50)) mylabel!. Text ="Closure" mylabel!. TextAlignment = Nstextalignment.center Self.View.addsubview (mylabel!) //DoAny additional setup after loading theView.}Func somefunctionthattakesaclosure (string:string), Void { function body goes here mylabel!. Text = string}Func nextbtnclicked () { LetSecond = 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 function  Second.initwithclosure (Somefunctionthattakesaclosure) Self.navigationController.pushViewController (Second,animated:True  } Override func Viewwilldisappear (Animated:bool) { mylabel!. Hidden =True}Override func Viewwillappear (Animated:bool) { mylabel!. Hidden =False}Override Func didreceivememorywarning () { Super.didreceivememorywarning () DisposeOfAny resources the can be recreated.}/ * //#pragma mark-navigation // in a storyboard-based application, you'll often want  to do a little preparation 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. }  */} 

Two, the second interface

Created by Qin Zhiwei on 14-6-13.import UIKitSimilar to the Typedeftypealias sendvalueclosure= in OC (string:string)->voidClass Zwsecondviewcontroller:uiviewcontroller {  var i:int? Declaring a closed packet var myclosure:sendvalueclosure? The following method needs to pass the Somefunctionthattakesaclosure function pointer to the previous interfaceFunc initwithclosure (closure:sendvalueclosure?) {  Assign a function pointer to the Myclosure closure, which covers references to local variables in the Somefunctionthattakesaclosure function, etc. Myclosure = closure} Init (nibname nibnameornil:string?, bundle Nibbundleornil:nsbundle?) { Super.init (Nibname:nibnameornil, Bundle:nibbundleornil)    Custom initialization} Override Func Viewdidload () { 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 sentence  If myclosure{   Closed Baoyin call somefunctionthattakesaclosure function: callback.  myclosure! (String"Good oh \ (i)") }} Override Func didreceivememorywarning () {  Super.didreceivememorywarning ()   //Dispose of any of the resources that can is recreated. }  /*// #pragma mark-navigation//In a storyboard-based application, you'll often want to do a little p Reparation before Navigation override func Prepareforsegue (Segue:uistoryboardsegue?, Sender: Anyobject?) { //Get The new view controller using [Segue destinationviewcontroller].< Span class= "indent" > //Pass The selected object to the new view Controller.}  */}              

Swift uses closures (closure) to deliver value-to-front and back-to-back values of two controllers

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.