• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Swift—闭包、代理实现反向传值(附具体代码)

武飞扬头像
酷酷的崽儿
帮助1

iOS开发过程中,我们经常会有这样的一个需求,假设有两个界面:ViewControllerA、ViewControllerB,我们从ViewControllerA进入到ViewControllerB,想把ViewControllerB中的值传回ViewControllerA(反向传值问题),下面介绍闭包和代理两种方法来处理.

这两种方法其实实现的思路是非常类似的,大致分为三个过程:定义、调用、实现.代理传值多一个设置代理的步骤.

闭包传值:

        定义:从哪个界面传值,就在哪个界面定义,本案例中是ViewControllerB.

        调用:从哪个界面传值,就在哪个界面调用,本案例中是ViewControllerB.

        实现:想传递到哪个界面,就在哪个界面实现,本案例中是ViewControllerA.

下面是具体实现代码:

        ViewControllerA:

  1.  
    import UIKit
  2.  
    class ViewControllerA: UIViewController{
  3.  
    override func viewDidLoad() {
  4.  
    self.view.backgroundColor = .white
  5.  
    }
  6.  
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  7.  
    let viewB = ViewControllerB()
  8.  
    self.present(viewB,animated: true)
  9.  
    //闭包实现
  10.  
    viewB.closure = { age,sentense in
  11.  
    print("\(age)------\(sentense)")
  12.  
    }
  13.  
    }
  14.  
    }

        ViewControllerB:

  1.  
    import UIKit
  2.  
    class ViewControllerB :UIViewController{
  3.  
    //闭包定义
  4.  
    var closure:((Int,String)->())?
  5.  
    override func viewDidLoad() {
  6.  
    self.view.backgroundColor = .brown
  7.  
    }
  8.  
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  9.  
    //闭包调用
  10.  
    closure!(1314,"Hyx Love Hxy")
  11.  
    self.dismiss(animated: true)
  12.  
    }
  13.  
    }

代理传值:

         ViewControllerA:        

  1.  
    import UIKit
  2.  
    class ViewControllerB :UIViewController{
  3.  
    //代理定义
  4.  
    var delegate:Transmit?
  5.  
    override func viewDidLoad() {
  6.  
    self.view.backgroundColor = .brown
  7.  
    }
  8.  
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  9.  
    //代理调用
  10.  
    delegate?.transmitData(year: 1314, sentense: "Hyx Love Hxy")
  11.  
    self.dismiss(animated: true)
  12.  
    }
  13.  
    }
  14.  
    //协议
  15.  
    protocol Transmit{
  16.  
    func transmitData(year:Int,sentense:String)
  17.  
    }
学新通

        ViewcontrollerB:

  1.  
    import UIKit
  2.  
    class ViewControllerB :UIViewController{
  3.  
    //代理定义
  4.  
    var delegate:Transmit?
  5.  
    override func viewDidLoad() {
  6.  
    self.view.backgroundColor = .brown
  7.  
    }
  8.  
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  9.  
    //代理调用
  10.  
    delegate?.transmitData(year: 1314, sentense: "Hyx Love Hxy")
  11.  
    self.dismiss(animated: true)
  12.  
    }
  13.  
    }
  14.  
    //协议
  15.  
    protocol Transmit{
  16.  
    func transmitData(year:Int,sentense:String)
  17.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbigcc
系列文章
更多 icon
同类精品
更多 icon
继续加载