iphone消息通信机制NSNotificationCenter
2011-11-24 14:55
357 查看
NSNotificationCenter是专门供程序中不同类间的消息通信而设置的,使用起来极为方便,
长话短说。
设置通知,就是说要在什么地方(哪个类)接受通知,一般在初始化中做。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"
test" object:nil];
我仅对以上参数做以说明:
addObserver 这个是观察者,就是说 在什么地方接收通知;
selector 这个是收到通知后,调用何种方法;
name: 这个是通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
发送通知,就是说此时要调用观察者处的方法。
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:searchFriendArray];
我仅对以上参数做以说明:
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:传递的参数
发送通知时,默认调用test方法。
- (void) test:(NSNotification*)
notification
{
searchFriendArrary = [notification object];//通过这个获取到传递的对象
}
=========================================================
例::
=========================================================
@implementation chuanzhenAppDelegate
-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
//根据接收到的不同,来发送不同的消息。此处发送Login
[[NSNotificationCenter defaultCenter]postNotificationName:@"Login" object:self];
}
另一BeforeRootViewController.m里。
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DidLogin) name:@"Login" object:nil];
}
-(IBAction) LoginClick
{
..............
}
长话短说。
设置通知,就是说要在什么地方(哪个类)接受通知,一般在初始化中做。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"
test" object:nil];
我仅对以上参数做以说明:
addObserver 这个是观察者,就是说 在什么地方接收通知;
selector 这个是收到通知后,调用何种方法;
name: 这个是通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
发送通知,就是说此时要调用观察者处的方法。
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:searchFriendArray];
我仅对以上参数做以说明:
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:传递的参数
发送通知时,默认调用test方法。
- (void) test:(NSNotification*)
notification
{
searchFriendArrary = [notification object];//通过这个获取到传递的对象
}
=========================================================
例::
=========================================================
@implementation chuanzhenAppDelegate
-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
//根据接收到的不同,来发送不同的消息。此处发送Login
[[NSNotificationCenter defaultCenter]postNotificationName:@"Login" object:self];
}
另一BeforeRootViewController.m里。
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DidLogin) name:@"Login" object:nil];
}
-(IBAction) LoginClick
{
..............
}
相关文章推荐
- [iPhone开发]消息通信机制NSNotificationCenter
- [iPhone开发]消息通信机制NSNotificationCenter
- iphone ios 消息通信机制NSNotificationCenter
- [iPhone开发]消息通信机制NSNotificationCenter
- iphone ios 消息通信机制NSNotificationCenter
- iPhone开发之消息通信机制NSNotificationCenter
- 详解iPhone开发之消息通信机制NSNotificationCenter
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- 【IOS】消息通信机制NSNotificationCenter
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- 进程通信-消息机制 .
- 消息通信机制NSNotificationCenter
- ceph源码分析之消息通信机制
- iPhone消息推送机制实现与探讨
- 进程间通信机制(管道、信号、共享内存/信号量/消息队列)、线程间通信机制(互斥锁、条件变量、posix匿名信号量)
- iPhone消息推送机制实现与探讨
- OpenStack建立实例完整过程源码详细分析(13)----依据AMQP通信架构实现消息发送机制解析之二
- NSNotificationCenter消息通信机制介绍(KVO)(转)
- iPhone消息推送机制实现与探讨