Swift uses the closure (closure) to implement the reverse value transfer between the two controllers --)

Source: Internet
Author: User

It took more than an hour to figure out the difference between reverse value transfer of Blocks in OC and Closure in Swift. The following code is directly pasted:

1. 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 !) // Do any additional setup after loading the view.} func someFunctionThatTakesAClosure (string: String)-> Void {// function body goes here myLabel !. Text = string} func nextBtnClicked () {let second = ZWSecondViewController (nibName: nil, bundle: nil) // transmits the pointer of the current someFunctionThatTakesAClosure function to the second interface, after the closure of the second interface obtains the function pointer, it calls back 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 () // Dispose of any resources that can be recreated .} /* // # pragma mark-Navigation // In a storyboard-based application, you will 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 .}*/}

2. Second Interface

// Created by Qin Zhiwei on 14-6-13.import UIKit // similar to typedeftypealias sendValueClosure = (string: String)-> Voidclass ZWSecondViewController: UIViewController {var I: Int? // Declare a closure var myClosure: sendValueClosure? // The following method needs to pass in the someFunctionThatTakesAClosure function pointer func initWithClosure (closure: sendValueClosure?) of the previous interface ?) {// Assign the function pointer to the myClosure closure, which contains references to local variables in the someFunctionThatTakesAClosure function, such as 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)? 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 {// The closure implicitly calls the someFunctionThatTakesAClosure function: callback. MyClosure! (String: "Good luck \ (I)")} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .} /* // # pragma mark-Navigation // In a storyboard-based application, you will 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 .}*/}

Reprinted please note !!!, Welcome to the iOS Communication Group: AI crazy, AI Coding: 209476515

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.