美文网首页
swift 闭包传值

swift 闭包传值

作者: 宁静1致远 | 来源:发表于2017-08-17 12:57 被阅读0次

闭包也能像OC中的Block一样进行反向传值下面直接上例子进行讲解
第一个界面

import UIKit

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.brownColor()
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let vc2 = ViewController2.init()
        
        vc2.myBlock={color in
            self.view.backgroundColor = color
        }
        
        self.presentViewController(vc2, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

第二个界面

import UIKit
typealias callBlockFunc = (color:UIColor)->()
class ViewController2: UIViewController {
    var myBlock = callBlockFunc?()
    override func touchesBegan(touches: Set<</span>UITouch>, withEvent event: UIEvent?) {
        myBlock!(color: UIColor.redColor())
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

相关文章

  • Swift:基础(十六)闭包

    Swift 闭包 闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。 Swift...

  • iOS swift 学习(二)

    Swift 闭包闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。Swift 中...

  • Swift-传值坑

    Swift中block、代理、通知、单例传值 block传值 定义一个闭包实现block主要分三步: 定义一个闭包...

  • Learning iOS D7 2017-10-30(传值方式

    Swift 4 四种传值方式 一:闭包传值(子vc传给父vc) 1.声明一个闭包(子vc) var closure...

  • swift 闭包传值

    在oc 中,反向传值可以采用block块来实现,同样,在swift 中也有类似的闭包,下面就闭包传值进行简单的介绍...

  • Swift基础 : 闭包

    Swift 闭包 闭包(Closures)是包含功能的代码块, 可以在代码中使用或者用来作为参数传值 闭包的定义:...

  • swift传值

    本文将介绍swift中的传值方式:属性传值、代理传值、闭包传值、通知传值本文将在两个VC之间进行传值:HomeVC...

  • Swift 闭包 传值

    语法 以下定义了一个接收参数并返回指定类型的闭包语法: { (parameters) -> return type...

  • swift 闭包传值

    场景:A页面跳转到B页面,B页面返回到A页面,(B页面给A页面传值) B页面逻辑:创建block,声明变量,传值 ...

  • swift 闭包传值

    闭包也能像OC中的Block一样进行反向传值下面直接上例子进行讲解第一个界面 第二个界面

网友评论

      本文标题:swift 闭包传值

      本文链接:https://www.haomeiwen.com/subject/afwgrxtx.html