Swift Closure (Closure) Callback pass value

Source: Internet
Author: User
Tags uikit



The implementation example consists of two interfaces
A-> B uses the attribute to pass the value
B-> A reverse callback value using closures



The principle of Swift's use of the closure (Closure) transfer value is basically similar to the principle of using code blocks (block) in OC.



Follow these steps to understand:
1, define closures.
2, Closure assignment (transfer)
3, closure call.



As for defining which page the closure should be defined on?



To perform an operation on the current interface, it is defined on the current interface,
For example, I want to assign a value to the text box on the A interface via the B-interface callback, and the assignment is performed on the A interface, then the closure should be defined on the A interface. Since the definition is in A, then the B interface is called the closure place. Find the implementation, followed by the caller, and then define the properties in the caller interface to receive closures;



Implementation code:



First Level interface A:


import UIKit

class ViewController: UIViewController {

    var textLab: UILabel?
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Create a text display lab
        textLab = UILabel (frame: CGRectMake (80, 50, 120, 40));
        textLab? .backgroundColor = UIColor.yellowColor ();
        textLab? .textAlignment = NSTextAlignment.Center;
        self.view.addSubview (textLab!);

        // Create a button to add event A-> B attribute pass value
        var nextBtn = UIButton (frame: CGRectMake (80, 120, 120, 40))
        nextBtn.setTitle ("Next Page", forState: UIControlState.Normal);
        nextBtn.backgroundColor = UIColor.redColor ();
        nextBtn.addTarget (self, action: "nextBtnAction", forControlEvents: UIControlEvents.TouchUpInside);
        self.view.addSubview (nextBtn);

    }
    // Define a closure with string parameters
    func myClosure (testStr: String)-> Void {
        // Assign textLab
        // When will this sentence be executed? , The closure is similar to the block in oc or can be understood as a function in c language, only when the content is called will be executed
        textLab? .text = testStr;
    }

    func nextBtnAction () {

        // Get the target page object
        var secondVC: SecondViewController = SecondViewController ();
        // Attribute assignment
        secondVC.str = "property pass value"
        // Transfer the closure to the secondary interface and call it in the secondary interface
        secondVC.testClosure = myClosure;
        // Modal view jump
        self .presentViewController (secondVC, animated: true, completion: nil);

    }


Second Level Interface B:


import UIKit

// Similar to typedef in OC
typealias sendValueClosure = (string: String)-> Void

class SecondViewController: UIViewController {

    var str: String?
    // Declare a closure
    var testClosure: sendValueClosure?

    override func viewDidLoad () {
        super.viewDidLoad ()

        // Create a text display lab
        var textLab = UILabel (frame: CGRectMake (80, 50, 120, 40));
        textLab.backgroundColor = UIColor.yellowColor ();
        textLab.textAlignment = NSTextAlignment.Center;
        // Assign value to the text box, this value comes from the previous interface
        textLab.text = str;
        self.view.addSubview (textLab);

        // Create a button to add event B-> A Closure callback pass value
        var backBtn = UIButton (frame: CGRectMake (80, 120, 120, 40))
        backBtn.setTitle ("Return", forState: UIControlState.Normal);
        backBtn.backgroundColor = UIColor.redColor ();
        backBtn.addTarget (self, action: "backBtnAction", forControlEvents: UIControlEvents.TouchUpInside);
        self.view.addSubview (backBtn);
    }

    func backBtnAction () {

        / **
        First determine whether the closure exists, and then call
        * /
        if (testClosure! = nil) {
            testClosure! (string: "Callback value")
        }

        self .dismissViewControllerAnimated (true, completion: nil)
    } 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Swift Closure (Closure) Callback pass value


Related Article

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.