[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.恢复数据(取消修改) 这个代码 ...
随机推荐
- Internet Explorer for Mac the Easy Way: Run IE 7, IE8, & IE9 Free in a Virtual Machine
From link: http://osxdaily.com/2011/09/04/internet-explorer-for-mac-ie7-ie8-ie-9-free/ If you’re ...
- hdu 4961 Boring Sum (思维 哈希 扫描)
题目链接 题意:给你一个数组,让你生成两个新的数组,A要求每个数如果能在它的前面找个最近的一个是它倍数的数,那就变成那个数,否则是自己,C是往后找,输出交叉相乘的和 分析: 这个题这种做法是O(n*s ...
- The dialect was not set. Set the property hibernate.dialect
The dialect was not set. Set the property hibernate.dialect load hibernate.cfg.xml 出 ...
- iOS开发:告诉git不要跟踪UserInterfaceState.xcuserstate
在xcode中使用git管理项目的技巧: 在多人协作开发的时候,每个开发者都会在项目中的某个目录生成一个 UserInterfaceState.xcuserstate 文件,这个文件大概每5s会刷新一 ...
- UVa 12034 (递推) Race
题意: 有n个人赛马,名次可能并列,求一共有多少种可能. 分析: 设所求为f(n),假设并列第一名有i个人,则共有C(n, i)种可能,接下来确定后面的名次,共有f(n-1)种可能 所以递推关系为: ...
- Html5大文件断点续传
大文件分块 一般常用的web服务器都有对向服务器端提交数据有大小限制.超过一定大小文件服务器端将返回拒绝信息.当然,web服务器都提供了配置文件可能修改限制的大小.针对iis实现大文件的上传网上也 ...
- 获取资源ID
比如,设置一张gif图片的宽高 gif.setShowDimension((int) CommonUtil.getDimen(R.dimen.gif), (int) CommonUtil.getDim ...
- Java Web编程的主要组件技术——Struts入门
参考书籍:<J2EE开源编程精要15讲> Struts是一个开源的Java Web框架,很好地实现了MVC设计模式.通过一个配置文件,把各个层面的应用组件联系起来,使组件在程序层面联系较少 ...
- iDSDT搞定显卡和声卡 黑苹果不纠结
原帖:http://www.lovelucy.info/idsdt-mac-video-audio-drive.html 之前写过PC机上装Mac OS X系统,准备工作中最纠结的就是驱动了.在网络上 ...
- Oracle数据文件管理
1.数据文件概述 Oracle数据库的数据文件(扩展名为DBF的文件)是用于保存数据库中数据的文件,系统数据.数据字典数据.临时数据.索引数据.应用数据等都物理地存储在数据文件中.用户对数据库中数据的 ...