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