swift中通知的使用
2023-11-25 23:00:52
1.发通知。(以这条通知为例,通知名字:gameOverNotification。通知参数:title)
NSNotificationCenter.defaultCenter().postNotificationName("gameOverNotification", object: title)
2.在要监听这则通知的viewDidload方法里面添加观察者,以便监听这则通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: "gameOver:", name: "gameOverNotification", object: nil)
3.实现2中的selector,监听到通知调用的方法
func gameOver(title:NSNotification)
{
var str = title.object as String
self.titleLable.text = str
}
提醒:此处的通知调用方法中的参数必须为NSNotification,因为我们发的就是1条通知。
友情提示:在swift中,如果对项目的逻辑结构不是很清楚一般用通知。
最新文章
- git 学习笔记
- SpringDataJPA的几个使用记录
- [bzoj2118]墨墨的等式【dijk+堆】
- phpWeb
- HTML link标签media参数
- 丢掉鼠标-Mac神软Alfred使用手册
- SDWebImage源码阅读-第三篇
- 用户管理-linux基础
- qt的信号与槽函数
- Git简单图文教程
- 从官方文档去学习之FreeMarker
- springboot之配置文件
- FPGA——入手(零)
- 官宣 ! 52abp_Pro版本低调上线
- Spring IOC基础使用
- golang 常用的正则查找与替换
- bp算法中为什么会产生梯度消失?
- 函数嵌套>;作用域>;闭包函数
- 怎样在MyEclipse上耍Chrome
- Hello Activemq