[iOS基础控件 - 6.10] Notification 通知机制

- - (NSString*)name; // 通知的名称
- - (id)object; // 通知发布者(是谁要发布通知)
- - (NSDictionary*)userInfo; // 一些额外的信息(通知发布者传递给通知接收者的信息内容)
- + (instancetype)notificationWithName:(NSString*)aName object:(id)anObject;
- + (instancetype)notificationWithName:(NSString*)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo;
- - (instancetype)initWithName:(NSString*)name object:(id)object userInfo:(NSDictionary *)userInfo;
- (void)dealloc {
//[super dealloc]; 非ARC中需要调用此句
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//
// Radio.h
// Notification
//
// Created by hellovoidworld on 14/12/7.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface Radio : NSObject // 电台名称
@property(nonatomic, copy) NSString *name; @end
//
// Radio.m
// Notification
//
// Created by hellovoidworld on 14/12/7.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "Radio.h" @implementation Radio @end
//
// main.m
// Notification
//
// Created by hellovoidworld on 14/12/7.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Radio.h"
#import "Person.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
Radio *r1 = [[Radio alloc] init];
r1.name = @"我方电台"; Radio *r2 = [[Radio alloc] init];
r2.name = @"敌方电台"; // 仅能接收我方电台消息
Person *p1 = [[Person alloc] init];
p1.role = @"我方忠心耿耿的战士"; // 仅能接收敌方电台消息
Person *p2 = [[Person alloc] init];
p2.role = @"敌方可恶的走狗"; // 技能接收我方电台消息,也能接收敌方电台消息
Person *p3 = [[Person alloc] init];
p3.role = @"潜伏在我方狡猾的内奸"; // 嗯,我是传播电台电波的媒介
NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; // 1.添加监听器
[center addObserver:p1 selector:@selector(receiveInfoFromRadio:) name:@"attack" object:r1];
[center addObserver:p2 selector:@selector(receiveInfoFromRadio:) name:@"defend" object:r2];
[center addObserver:p3 selector:@selector(receiveInfoFromRadio:) name:@"attack" object:r1];
[center addObserver:p3 selector:@selector(receiveInfoFromRadio:) name:@"defend" object:r2]; // 2.我方电台r1发布攻击信息
[center postNotificationName:@"attack" object:r1 userInfo:@{@"title":@"attack!, all the money belong to us!", @"title2":@"And the girls!!!"}]; // 3.敌方电台r2发布回防信息
[center postNotificationName:@"defend" object:r2 userInfo:@{@"title":@"TP back quickly!"}]; }
return ;
}
title = "attack!, all the money belong to us!";
title2 = "And the girls!!!";
}
2014-12-07 21:18:03.143 Notification[7923:714964] 我是潜伏在我方狡猾的内奸, 正在接受我方电台的消息, 内容是{
title = "attack!, all the money belong to us!";
title2 = "And the girls!!!";
}
2014-12-07 21:18:03.143 Notification[7923:714964] 我是敌方可恶的走狗, 正在接受敌方电台的消息, 内容是{
title = "TP back quickly!";
}
2014-12-07 21:18:03.143 Notification[7923:714964] 我是潜伏在我方狡猾的内奸, 正在接受敌方电台的消息, 内容是{
title = "TP back quickly!";
}
[iOS基础控件 - 6.10] Notification 通知机制的更多相关文章
- [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo
A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用 B.实现步骤 1.准备plist文件和国旗图片 2.创建模型 // // Flag.h // Co ...
- [iOS基础控件 - 6.10.5] UIApplication
A.概念 1.UIApplication对象是应用程序的象征,每个应用都有 2.单例 3.[UIApplication sharedApplication] 获取 4.iOS启动创建的第一个对象 5. ...
- [iOS基础控件 - 6.10.7] UIWindow
A.UIWindow概念 1.继承UIView,是一种特殊的UIView 2.通常一个APP只有一个UIWindow 3.iOS程序启动后,创建的第一个视图就是UIWindow 4.没有UIWindo ...
- [iOS基础控件 - 6.10.6] UIApplicationDelegate & 程序启动过程
A.概念 1.移动app非常容易受到其他的系统.软件事件的干扰,如来电.锁屏 2.app受到干扰的时候,UIApplication会通知delegate,来代理处理干扰事件 3.delegate可以处 ...
- [iOS基础控件 - 6.10.1] PickerView 餐点搭配Demo
A.需求 1.使用PickerView做出有3列餐点(水果.主菜.饮料)的搭配Demo 2.选择的餐点实时显示在“显示区” 3.提供“随机”按钮,随机选择菜品搭配 B.实现步骤 1.拖入一个Pic ...
- [iOS基础控件 - 6.10.4] 项目启动原理 项目中的文件
A.项目中的常见文件 1.单元测试Test 2.Frameworks(xCode6 创建的SingleView Project没有) 依赖框架 3.Products 打包好的文件 4. p ...
- [iOS基础控件 - 6.10.3] DatePicker & UIToolBar
A.需求 1. 学习DatePicker的基本配置 2.使用TextField召唤指定类型的输入键盘View,这里使用DatePicker 3.给输入键盘上方加上一个UIToolBar,实现如关闭键盘 ...
- [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)
A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不 ...
- [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储
A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改) 这个代码 ...
随机推荐
- [POJ3352]Road Construction(缩点,割边,桥,环)
题目链接:http://poj.org/problem?id=3352 给一个图,问加多少条边可以干掉所有的桥. 先找环,然后缩点.标记对应环的度,接着找桥.写几个例子就能知道要添加的边数是桥的个数/ ...
- 面试题_82_to_87_Date、Time 及 Calendar 的面试题
82)在多线程环境下,SimpleDateFormat 是线程安全的吗?(答案)不是,非常不幸,DateFormat 的所有实现,包括 SimpleDateFormat 都不是线程安全的,因此你不应该 ...
- struts.custom.i18n.resources 如何配置多个资源文件?
struts.custom.i18n.resources = resources1,resources2,resources3 配置properties文件
- Alpha、Beta、RC、GA版本的区别
Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用. Beta:也是测试版,这个阶段的版本会一直加入新的功能.在Alpha版之后推出. RC:(Release Candi ...
- sharepoint Linq方式的增,删,查,改
Site9527EntitiesDataContext (重要的类):连接实体与网站List操作 SPContext.Current.Web.Url:获取当前操作的页面 FirstOrDefault: ...
- bzoj4177: Mike的农场
类似于最大权闭合图的思想. #include<cstdio> #include<cstring> #include<iostream> #include<al ...
- 使用源代码安装lnmp
一.安装nginx前,安装pcre. # tar zxvf pcre-8.12.tar.gz# ./configure# make# make install 二.安装nginx # tar zxvf ...
- Android好用且常用的插件及工具
1.GitHub,这个不管是做安卓还是其他,只要是开发就必上的网站,也是天朝没有墙掉为数不多的网站 2.Stack OverFlow,这个和上面一样,国外非常著名的问答网站,在上面基本上很多问题都可以 ...
- mysql explain中key_len的计算
ken_len表示索引使用的字节数,根据这个值,就可以判断索引使用情况,特别是在组合索引的时候,判断是否所有的索引字段都被查询用到. key_len显示了条件检索子句需要的索引长度,但 ORDER B ...
- 联通光纤上网配置+华为HG8240光猫+TL-WR842N
最近搬家改用北京联通宽带,光纤入户的那种.联通送的光猫是华为HG8240,没看到天线,应该是不带无线路由.然后自己再买了个TP-Link的TL-WR842N,用来组局域网,也供ipad.kindle. ...