- (void)viewDidLoad {

[super viewDidLoad];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(100, 100, 100, 50);

btn.backgroundColor = [UIColor grayColor];

[btn setTitle:@"click me" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

// 注册消息

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNoticeMessage:) name:@"test" object:@"notice-recieve"];

}

-(void)clickAction:(id)sender

{

// 能接受消息

[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:@"notice-recieve"];

// 不能接收到消息,object 对象不一致

[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:@"notice-recieve111"];

}

// 处理消息

-(void)handleNoticeMessage:(id)sender

{

NSLog(@"handleNoticeMessage sender = %@", sender);

}

-(void)viewWillDisappear:(BOOL)animated

{

// 移除通知

[[NSNotificationCenter defaultCenter]removeObserver:self];

}

NSNotificationCenter的更多相关文章

  1. iOS NSNotificationCenter详解

    通知中心的特点: 1:同步执行 2: 一对多发送消息 3: 降低程序耦合度 通知中心是单例,目的就是从任意一个发送消息到任意一个接收者,是同步执行的. 那么什么是同步呢? 用网上经典的说法,就是我叫朋 ...

  2. iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项

    我们在开发中会遇到这样的情况:调用键盘时需要界面有一个调整,避免键盘遮掩输入框. 但实现时你会发现,在不同的手机上键盘的高度是不同的.这里列举一下: //获取键盘的高度 /* iphone 6: 中文 ...

  3. IOS 消息机制(NSNotificationCenter)

    消息机制 NSNotificationCenter 一直都在频繁使用,但是却对其原理不是十分了解.今天就花些时间,把消息机制原理重头到尾好好过一遍. iOS 提供了一种 "同步的" ...

  4. IOS学习之NSNotificationCenter消息机制

    NSNotificationCenter是 Cococa消息中心,统一管理单进程内不同线程的消息通迅. 添加观察者接收通知: //添加通知中心观察者 [[NSNotificationCenter de ...

  5. 通知中心 NSNotificationCenter 的简单使用方法

    NSNotificationCenter(通知中心)   [注意]需再dealloc中移除观察者   获取通知中心单例对象 NSNotificationCenter *center=[NSNotifi ...

  6. Swift利用协议优化NSNotificationCenter

    NSNotificationCenter存在的问题 通知没有统一的命名格式 对于通知的命名没有强制的要求,一个项目里可能有多种不同的命名规则.比如: 1 2 3 4 5 6 class Barista ...

  7. 通知 - NSNotificationCenter

    1.每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信: 2.任何一个对象都可以向通知中心发布通知(NSNotification), 描 ...

  8. NSNotificationCenter应用总结

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad.  Apple 还为我们提供了另一种通知响应方式,那就是 NSNot ...

  9. 你真的了解NSNotificationCenter吗?

    一:首先查看一下关于NSNotificationCenter的定义 @interface NSNotificationCenter : NSObject { @package void * __str ...

  10. iOS中通知中心(NSNotificationCenter)的使用总结

    一.了解几个相关的类 1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readon ...

随机推荐

  1. 在 MySQL 中查找含有目标字段的表

    要查询数据库中哪些表含有目标字段,可以使用语句: SELECT TABLE_SCHEMA,TABLE_NAME FROM information_schema.`COLUMNS` WHERE COLU ...

  2. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [4] 首页 APP 接口开发方案 ③ 定时读取缓存方式

    用于 linux 执行 crontab 命令生成缓存的文件 crop.php <?php //让crontab 定时执行的脚本程序 require_once 'db.php'; require_ ...

  3. PHP常用验证正则表达式

    PHP常用验证正则表达式 数字.手机号.QQ号.Url地址合法性校验 1.验证是否为整数 1 function isNumber($val) 2 { 3 if(ereg("^[0-9]+$& ...

  4. RT-Thread信号量实际运用—按键点灯

    上面是魔笛开发板上 LED 和按键的 IO 分布,我们通过信号量的方法来同步按键线程和LED 线程,实现当 enter 键按下后,点亮或关闭 LED 的动作. /******************* ...

  5. 实用Javascript代码片段

    清除select下拉选项,添加并选择特点选项 $('#mySelect') .find('option') .remove() .end() .append('<option value=&qu ...

  6. varnish中忽略cookie进行缓存

    varnish不缓存cookie的页面,如果html页面中带有cookie以下代码为接收到结尾的文件,自动去除掉cookiesub vcl_recv {    if (req.request == ” ...

  7. 9.PHP内核探索:通过mod_php5支持PHP

    Apache对PHP的支持是通过Apache的模块mod_php5来支持的.如果希望Apache支持PHP的话,在./configure步 骤需要指定--with-apxs2=/usr/local/a ...

  8. mysql查询昨天本周上周上月

    昨天 $yestoday = date("Y-m-d 00:00:00",strtotime('-1day'));$today = date("Y-m-d 00:00:0 ...

  9. [dpdk] 读开发指南(1)

    该文档是随着对于文档的阅读进度,不断增加的阅读笔记.主要内容以大纲为主,以及记录帮助记忆的内容. 在之后的实际应用中,也不随着不断的深入理解,逐渐丰富各大纲下面的内容. 1. 前期准备:设置两个环境变 ...

  10. go安装windows源码

    直接安装就好,下面是安装地址http://pan.baidu.com/s/1gdDFi9t