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