加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift开发:NSNotificationCenter 传值

发布时间:2020-12-14 06:21:35 所属栏目:百科 来源:网络整理
导读:在app开发过程中,需要使用到NSNotificationCenter传递数据 第一步: 注册通知 NSNotificationCenter . defaultCenter (). addObserver ( self ,selector: "sendCMD:" ,name: "sendCMD" ,object: nil ); sendCMD 是接受通知时的接受函数,名字是通知的key fu

在app开发过程中,需要使用到NSNotificationCenter传递数据

第一步: 注册通知

NSNotificationCenter.defaultCenter().addObserver(self,selector: "sendCMD:",name: "sendCMD",object: nil);


sendCMD 是接受通知时的接受函数,名字是通知的key

func sendCMD(obj: NSNotification)

{

let da:String? = obj.object as! String;// hello

// da就是数据,发送什么类型的数据 da就定义什么类型

}


注意移除通知

deinit {

NSNotificationCenter.defaultCenter().removeObserver(self);

}



第二部:发送通知

NSNotificationCenter.defaultCenter().postNotificationName("sendCMD",object:"hello")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读