[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.恢复数据(取消修改) 这个代码 ...
随机推荐
- [转载]Python模块学习 ---- subprocess 创建子进程
[转自]http://blog.sciencenet.cn/blog-600900-499638.html 最近,我们老大要我写一个守护者程序,对服务器进程进行守护.如果服务器不幸挂掉了,守护者能即时 ...
- 关于<img>标签与文字垂直居中
要让左边的图片与后面的文字居中,如下效果 HTML代码: <img class="iconCls" alt="最新客户端" src="${bas ...
- highcharts 饼图显示数据比例如何保留二位小数
var NewPerCent=parseFloat(NewPerCent.toString()).toFixed(2);return '<b>'+ this.point.name +'&l ...
- ASP.NET在IE10,IE11中Form表单身份验证失效问题解决方法
已经研究出解决方案. 在web.config中的forms中增加cookieless="UseCookies"属性即可. <authentication mode=&qu ...
- HTMLParser 解析HTML
from html.parser import HTMLParser from html.entities import name2codepoint class MyHTMLParser(HTMLP ...
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- ORACLE之手动注册监听listener。alter system set local_listener="XXX"
记录下刚刚做的一个为一个数据库(t02)配置多个监听(listener)的实验,过程有点小曲折. (1)新增两个测试的监听,listener.ora的配置内容(可纯手动编辑该文件或使用netca)如下 ...
- MySql表中key的区别
我们看到Key那一栏,可能会有4种值,即'啥也没有','PRI','UNI','MUL'1. 如果Key是空的, 那么该列值的可以重复, 表示该列没有索引, 或者是一个非唯一的复合索引的非前导列2. ...
- 音频PCM格式
经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit stereo: 每秒钟有 44100 次采样, 采样数据用 ...
- hdu 2845(dp基础题)
题意:容易理解. 分析:以后碰到这种类型的题,就要考虑把矩阵先按行来处理,再按列处理.先算出每行能够能够得到的最大值,然后按列处理即可. 代码实现: #include<stdio.h> # ...