#import "Listener.h"
@implementation Listener
- (void)wantToListener
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recvBroadcast:) name:@"test one" object:nil];
}
- (void)recvBroadcast:(NSNotification *)notify
{
NSLog(@"notify is %@", notify);
}
@end
#import "Broadcase.h"
@implementation Broadcase
- (void)broadcast
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
static int i;
NSString *count = [NSString stringWithFormat:@"broadcase %i", i ++];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"bj broadcase", @"Name", count, @"value", nil];
[nc postNotificationName:@"test one" object:self userInfo:dict];
}
- (void)broadcastloop
{
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(broadcast) userInfo:nil repeats:YES];
}
@end
Broadcase *broad = [[Broadcase alloc]init];
[broad broadcastloop];
Listener *listener1 = [[Listener alloc]init];
[listener1 wantToListener];
Listener *listener2 = [[Listener alloc]init];
[listener2 wantToListener];
[[NSRunLoop currentRunLoop] run];