Swift闭包回传的实例

1首先在 AppDelegate.swift 中给控制器添加导航栏

    //  Created by Zin_戦 on 16/6/15.
//  Copyright © 2016年 Zin戦壕. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
    self.window!.backgroundColor = UIColor.redColor()
    self.window!.makeKeyAndVisible()
    //创建一个控制器
    let Vc = ViewController()
    let NVc = UINavigationController(rootViewController:Vc)
    
    self.window?.rootViewController = NVc
    return true
}

2.接下来在ViewController里面

//  Created by Zin_戦 on 16/6/15.
//  Copyright © 2016年 Zin戦壕. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

/*
 懒加载格式:
 lazy var 变量名:变量类型 = {
 let 名字 = 变量类型()
 return 名字
 }()
 */

lazy var MyBtn : UIButton = {
    let MyBtn = UIButton(frame:CGRectMake(150,150,50,50))
    MyBtn.backgroundColor = UIColor.grayColor()
    MyBtn.setTitle("跳转", forState: UIControlState.Normal)
    MyBtn.addTarget(self, action: #selector(ViewController.MyBtnAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)

    return MyBtn
    
}()

lazy var MyLab:UILabel = {
    let MyLab = UILabel(frame:CGRectMake(200,200,60,40))
    MyLab.backgroundColor = UIColor.greenColor()
    MyLab.text = "两颗草"
    MyLab.adjustsFontSizeToFitWidth = true
    return MyLab
    
}()
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    view.addSubview(MyBtn)
    view.addSubview(MyLab)
}



func MyBtnAction(btn:UIButton) {
    print("按钮被点击了")
    let secVc = SecondVC()
    //闭包传值-3.实现闭包方法,并执行闭包
    secVc.closure = {
        (str:String) -> ()
        in
        self.MyLab.text = str
    }
    
    navigationController?.pushViewController(secVc, animated: true)
    
}
}

3.创建一个SecondVc

//  Created by Zin_戦 on 16/6/15.
//  Copyright © 2016年 Zin戦壕. All rights reserved.
//

import UIKit

class SecondVC: UIViewController {

//闭包生命1.声明闭包方法
var closure:((str:String) ->())?

//懒加载lab
lazy var lab : UILabel = {
    var lab = UILabel(frame:CGRectMake(100,100,60,40))
    lab.backgroundColor = UIColor.greenColor()
    lab.text = "一颗草"
    lab.adjustsFontSizeToFitWidth = true
    return lab
}()

//懒加载Button
lazy var btn : UIButton =
    {
        let btn = UIButton(frame:CGRectMake(180,180,60,69))
        btn.backgroundColor = UIColor.brownColor()
        btn.setTitle("传值", forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(SecondVC.btnAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
}()

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = UIColor.whiteColor()
    view.addSubview(lab)
    view.addSubview(btn)
}

func btnAction(btn:UIButton) {
    //闭包传值2.调用闭包传值
    closure!(str:lab.text!)
    navigationController?.popViewControllerAnimated(true)
}

}

tips:1.这里的闭包可以随便定义什么名字;

2.这里的懒加载固定的语法块,不要忘了{}()哦

/*
 懒加载格式:
 lazy var 变量名:变量类型 = {
 let 名字 = 变量类型()
 return 名字
 }()
 */

3.总结闭包回传的步骤,类比block

1》在要把值传给另一个控制器的控制器(SecondVc)里声明闭包方法
2》在SecondVc里调用闭包传值
3》在FirstVc实现闭包方法,并执行闭包

最后吐槽,Xcode7.3.1提示功能太蛋疼,Xcode8正式版快出来吧,beat版已经解决这个问题了

你可能感兴趣的:(Swift闭包回传的实例)