[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.恢复数据(取消修改) 这个代码 ...
随机推荐
- mysql定时计划任务,ON COMPLETION [NOT] PRESERVE 当单次计划任务执行完毕后或当重复性的计划任务执行到了ENDS阶段。而声明PRESERVE的作用是使事件在执行完毕后不会被Drop掉
当为on completion preserve 的时候,当event到期了,event会被disable,但是该event还是会存在当为on completion not preserve的时候,当 ...
- 使用easyui实现列表的批量删除
使用easyui实现列表的批量删除 首先要做的就是增加一个多选框 <table id="otGrid" nowrap="false" style=&quo ...
- linux关机和重启的命令[转]
如果你很急着关机或者重启话,那么关机就是init 0,重启就是init 6或者reboot Linux中常用的关机和重新启动命令有shutdown.halt.reboot以及init,它们都可以达到关 ...
- 函数buf_page_create
/********************************************************************//** Initializes a page to the ...
- EF DataBase First生成model的验证
如何避免在EF自动生成的model中的DataAnnotation被覆盖掉 相信很多人刚接触EF+MVC的时候,DataBase First模式生成model类中加验证信息的时候,会在重新生成mode ...
- Android SharedPreferences 权限设置
说明: 由于目前打算采用两个app来完成一件事,采用SharedPreferences来做数据交换,于是突然想验证一下Java层的权限设置会不会就是设置Linux下文件的权限,验证的结果是这样的. T ...
- 【linux】命令
pwd 显示路径 whereis jupyterhub find / -name base.py reboot 重启 grep
- virtualbox更新完之后重启不成功
前几天更新完virtualbox,一直没用,今天想用,可是提示can't not access the kernel drivers,百度完之后按照别人博客所教方法弄好了,特地来转载他人文章,表达对博 ...
- iOS开发实践:一个类微博客户端从启动到与用户交互的过程
本文基于数据字典和数据流图两种工具讲述一个完整微博客户端的实现.数据字典和数据流图都可以用来表达线程的执行流程,同时定义了需要的类,是进一步设计类的基础. 数据字典实际上是一张表,表的第一个字段是程序 ...
- poj 1087 A Plug for UNIX
题目描述:现在由你负责布置Internet联合组织首席执行官就职新闻发布会的会议室.由于会议室修建时被设计成容纳全世界各地的新闻记者,因此会议室提供了多种电源插座用以满足(会议室修建时期)各国不同插头 ...