美文网首页
swift 代理传值

swift 代理传值

作者: 旅途开发者 | 来源:发表于2016-12-12 16:58 被阅读140次

swift中的代理传值跟oc中的用法基本一样,都是用于反向传值,这里假设a界面向b界面传值为正向传值,则:

在b界面

//设置代理方法

protocol BaseOneControllerDelegate {

          func sendMessage(message:NSString?)

}

class BaseOneController: UIViewController {

//定义代理属性

var delegate:BaseOneControllerDelegate?

override func viewDidLoad() {

super.viewDidLoad()

self.view.backgroundColor = UIColor.white

let segMentArray = ["代理返回"]

segment = UISegmentedControl(items: segMentArray)

segment.frame = CGRect.init(x: 0, y: 100, width: kWidth, height: 30)

//添加动作事件

segment.addTarget(self, action: #selector(BaseOneController.segmentAction(sender:)), for: UIControlEvents.valueChanged)

self.view.addSubview(segment)

}

func segmentAction(sender:UISegmentedControl) {

print("我点击了第 \(sender.selectedSegmentIndex) 选项")

  if sender.selectedSegmentIndex == 0 {

//完成代理事件

      if (delegate != nil) {

        self.delegate?.sendMessage(message: "代理传值成功,点击返回按钮")

      _ = self.navigationController?.popViewController(animated: true)

  }

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

在a界面,首先添加b界面的代理BaseOneControllerDelegate

let baseOne = BaseOneController()

//跳转的时候记得设置好代理

baseOne.delegate = self

self.navigationController?.pushViewController(baseOne, animated: true)

//代理方法

func sendMessage(message: NSString?) {

print("\(message)")

}

相关文章

  • OC与swift的数据传输

    简介 该项目主要介绍了oc与swift之间、swift内部几种常见的传值方式(属性传值、代码块传值、代理传值、通知...

  • Swift界面传值

    Swift中界面传值的方法 主要有三种 1.代理传值2.闭包传值(即OC中的Block) 属性传值 代理传值 F...

  • swift传值

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

  • OC、swift混编中的反向传值

    一 OC向swift传值 1) 代理 1.1在oc中创建 代理 #import @protocolSecon...

  • iOS之传值

    在iOS中传值的方式有很多种方式,有最普遍的就是属性传值,代理传值,block传值等方式了。写了OC和swift的...

  • swift 代理传值

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

  • swift 代理传值

    swift中的代理传值跟oc中的用法基本一样,都是用于反向传值,这里假设a界面向b界面传值为正向传值,则: 在b界...

  • swift传值:利用代理(delegate)和闭包(closu

    转载Swift使用delegate和closure进行传值:类似oc的代理和block firstViewCont...

  • Swift 代理传值方式

    协议传值 协议传值,主要用于代理模式。假设我们要实现从详情界面传值到主界面这一需求,首先,我们需要拟定一份协议,为...

  • Swift常用的界面传值(属性传值、协议传值、闭包传值)

    1、属性传值 属性传值多用于正向传值(A->B) 2、代理传值 代理传值多用于反向传值(B->A) 3、闭包传值 ...

网友评论

      本文标题:swift 代理传值

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