iOS 消息通知-NSNotification
2013-08-20 15:26
288 查看
ios的消息通知时通过一个消息中心来负责发送的
现在给一个 国王发送圣旨给 农民和工人的例子
=====================================
King类
#import "King.h"
@implementation King
-(void)SendMess
{
NSLog(@"圣旨发出");
//创建通知对象
通知的名字是: NOTIFICATIONNAME
NSNotification *notify =
nil;
notify = [NSNotification
notificationWithName:@"NOTIFICATIONNAME"
object:self
userInfo:[NSDictionary
dictionaryWithObject:@"孩儿们 要征税了 水深火热去吧!" forKey:@"order"]];
//消息中心
发送通知
[[NSNotificationCenter
defaultCenter] postNotification:notify];
}
@end
=====================================
Worker类
#import "Worker.h"
@implementation Worker
-(id)init
{
if(self = [super
init])
{
//注册监听者
通知的名字是: NOTIFICATIONNAME
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(receiveMes:)
name:@"NOTIFICATIONNAME"
object:nil];
}
return
self;
}
-(void)receiveMes:(NSNotification *)notify
{
//接收到消息中心送来的
用户信息
NSDictionary *dic = [notify
userInfo];
NSLog(@"Worker:收到信息:%@",[dic
objectForKey:@"order"]);
}
-(void)dealloc
{
//除移通知
通知的名字是: NOTIFICATIONNAME
[[NSNotificationCenter
defaultCenter] removeObserver:self
forKeyPath:@"NOTIFICATIONNAME"
context:nil];
[super
release];
}
@end
=====================================
Farmer类 (和Worker类是一样的)
#import "Farmer.h"
@implementation Farmer
-(id)init
{
if(self=[super
init])
{
//注册监听者
通知的名字是: NOTIFICATIONNAME
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(receiveMsg:)
name:@"NOTIFICATIONNAME"
object:nil];
}
return
self;
}
-(void)receiveMsg:(NSNotification *)notify
{
NSDictionary *dic = [notify
userInfo];
NSLog(@"Farmer:收到信息:%@",[dic objectForKey:@"order"]);
}
-(void)dealloc
{
//除移通知
通知的名字是: NOTIFICATIONNAME
[[NSNotificationCenter
defaultCenter] removeObserver:self
forKeyPath:@"NOTIFICATIONNAME"
context:nil];
[super dealloc];
}
@end
=====================================
main
#import <Foundation/Foundation.h>
#import "King.h"
#import "Worker.h"
#import "Farmer.h"
int main(int argc,
const char * argv[])
{
@autoreleasepool {
King *king = [King
new];
Farmer *farmer = [Farmer
new];//new 等价于 alloc init 所以会执行到 重写的init方法
Worker *work = [Worker
new]; //new 等价于 alloc init 所以会执行到 重写的init方法
[king
SendMess];
}
return 0;
}
结果
2013-08-20 15:25:38.559 demo[3665:303]
圣旨发出
2013-08-20 15:25:38.561 demo[3665:303] Farmer:收到信息:孩儿们要征税了水深火热去吧!
2013-08-20 15:25:38.562 demo[3665:303] Worker:收到信息:孩儿们要征税了水深火热去吧!
现在给一个 国王发送圣旨给 农民和工人的例子
=====================================
King类
#import "King.h"
@implementation King
-(void)SendMess
{
NSLog(@"圣旨发出");
//创建通知对象
通知的名字是: NOTIFICATIONNAME
NSNotification *notify =
nil;
notify = [NSNotification
notificationWithName:@"NOTIFICATIONNAME"
object:self
userInfo:[NSDictionary
dictionaryWithObject:@"孩儿们 要征税了 水深火热去吧!" forKey:@"order"]];
//消息中心
发送通知
[[NSNotificationCenter
defaultCenter] postNotification:notify];
}
@end
=====================================
Worker类
#import "Worker.h"
@implementation Worker
-(id)init
{
if(self = [super
init])
{
//注册监听者
通知的名字是: NOTIFICATIONNAME
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(receiveMes:)
name:@"NOTIFICATIONNAME"
object:nil];
}
return
self;
}
-(void)receiveMes:(NSNotification *)notify
{
//接收到消息中心送来的
用户信息
NSDictionary *dic = [notify
userInfo];
NSLog(@"Worker:收到信息:%@",[dic
objectForKey:@"order"]);
}
-(void)dealloc
{
//除移通知
通知的名字是: NOTIFICATIONNAME
[[NSNotificationCenter
defaultCenter] removeObserver:self
forKeyPath:@"NOTIFICATIONNAME"
context:nil];
[super
release];
}
@end
=====================================
Farmer类 (和Worker类是一样的)
#import "Farmer.h"
@implementation Farmer
-(id)init
{
if(self=[super
init])
{
//注册监听者
通知的名字是: NOTIFICATIONNAME
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(receiveMsg:)
name:@"NOTIFICATIONNAME"
object:nil];
}
return
self;
}
-(void)receiveMsg:(NSNotification *)notify
{
NSDictionary *dic = [notify
userInfo];
NSLog(@"Farmer:收到信息:%@",[dic objectForKey:@"order"]);
}
-(void)dealloc
{
//除移通知
通知的名字是: NOTIFICATIONNAME
[[NSNotificationCenter
defaultCenter] removeObserver:self
forKeyPath:@"NOTIFICATIONNAME"
context:nil];
[super dealloc];
}
@end
=====================================
main
#import <Foundation/Foundation.h>
#import "King.h"
#import "Worker.h"
#import "Farmer.h"
int main(int argc,
const char * argv[])
{
@autoreleasepool {
King *king = [King
new];
Farmer *farmer = [Farmer
new];//new 等价于 alloc init 所以会执行到 重写的init方法
Worker *work = [Worker
new]; //new 等价于 alloc init 所以会执行到 重写的init方法
[king
SendMess];
}
return 0;
}
结果
2013-08-20 15:25:38.559 demo[3665:303]
圣旨发出
2013-08-20 15:25:38.561 demo[3665:303] Farmer:收到信息:孩儿们要征税了水深火热去吧!
2013-08-20 15:25:38.562 demo[3665:303] Worker:收到信息:孩儿们要征税了水深火热去吧!
相关文章推荐
- iOS开发-消息通知机制(NSNotification和NSNotificationCenter)
- IOS NSNotification Center 通知中心的使用
- IOS 开发过程中的 消息通知 小红点
- iOS PAD编写 POPOver 使用tableviewcontroller的两种操作方式,代理和消息通知
- iOS开发系列--通知与消息机制
- iOS 系统消息通知归纳
- IOS 开发过程中的 消息通知 小红点
- iOS 通知中心 NSNotificationCenter & NSNotification
- (七十二)自定义通知NSNotification实现消息传递
- 转载大神IOS开发系列【14】--通知与消息机制
- iOS NSNotificationCenter 移除通知带来的crash
- iOS 通知中心 NSNotificationCenter & NSNotification
- iOS 点击远程通知消息,如何跳转到指定页面(控制器)
- iOS 消息推送通知
- iOS UI界面之间传值方式的整理总结(3)通知传值NSNotification
- IOS 新消息通知提示-声音、震动
- iOS设备通知中心精品推荐消息删除
- iOS开发系列--通知与消息机制
- 【IOS】IOS8推送消息注册 通知
- IOS 新消息通知提示-声音、震动