美文网首页
swift 通知传值

swift 通知传值

作者: 勇敢的我2017 | 来源:发表于2018-09-13 10:01 被阅读0次

场景:A页面跳转到B页面,B页面返回到A页面,(B页面给A页面传值)

B页面逻辑:发送通知

A页面逻辑:创建通知,移除通知

B页面代码:

  @IBAction func back(_sender:UIButton) {

          let  dic = ["name":passTf.text??""]

        //多值传递

        NotificationCenter.default.post(name:NSNotification.Name(rawValue:"value"), object:nil, userInfo: dic)

        //单值传递

        NotificationCenter.default.post(name:NSNotification.Name(rawValue:"passValue"), object:"zyy",userInfo:nil)

        self.navigationController?.popViewController(animated:true)

    }

A页面代码: 

override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        self.title="第一页"

        NotificationCenter.default.addObserver(self, selector:#selector(change(info:)), name:NSNotification.Name(rawValue:"value"), object:nil)

         NotificationCenter.default.addObserver(self, selector:#selector(change1(info:)), name:NSNotification.Name(rawValue:"passValue"), object:nil)

    }

   @objc  func change(info:NSNotification)  {

    print(info.userInfo) // 类型为[AnyHashable,Any]

    let dic = info.userInfo as! Dictionary

    textLabel.text = dic[AnyHashable("name")] as! String

    }

    @objc  func change1(info:NSNotification)  {

        print(info.object)

    }

相关文章

  • OC与swift的数据传输

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

  • swift传值

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

  • swift 通知传值

    场景:A页面跳转到B页面,B页面返回到A页面,(B页面给A页面传值) B页面逻辑:发送通知 A页面逻辑:创建通知,...

  • SWIFT 通知传值

    通知传值 首先我们来看看通知传值,通知可实现任意界面之间的数据传递,但必须满足一个条件,就是保证在发送通知的时候监...

  • swift 通知传值

    B 往 A 传值 在 B 里面 NotificationCenter.default.post(name:...

  • Swift通知传值

    如果您在阅读我的文章时有疑问 , 请点击这里通知方 接受方

  • 常见的通知

    一、通知传值: 二、通知不传值:

  • Swift-传值坑

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

  • Swift的四种传值方法

    Swift中常用的四种传值方法:单例,单例,闭包(相当于OC的block传值),通知 1单例: 1>.创建单例变量...

  • 11.4页面传值

    //一、AppDelegate.swift // //AppDelegate.swift //页面传值 // //...

网友评论

      本文标题:swift 通知传值

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