#import “BJbroadcast.h”
@implementation BJbroadcast
-(void)broadcast{
static int i;
//1.取得消息中心
NSNotificationCenter *nc=[NSNotificationCenter defaultCenter];
NSString *count=[NSString stringWithFormat:@”bcast %d”,i++];
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@”BJ broadcast“,@”Name”,count,@”Value”, nil];
//消息内容
//2.发送广播
[nc postNotificationName:@”BJbroadcast” object:self userInfo:dict];
}
@end
#import “Lietener.h”
@implementation Lietener
-(void)wantTolisten{
//1.注册
[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(recvBcast:)
name:@”BJBroadcast” object:nil];
//param1,param2 这两个参数,只要有BJBroadcast广播就调用[self recvBcast];
//4个参数一般都设为nil
//2.要真正的接收广播数据
}
-(void)recvBcast:(NSNotification *)notify{
//notify就是具体的广播消息
NSLog(@”notify is %@”,notify);
}
@end
#import <Foundation/Foundation.h>
#import “BJbroadcast.h”
#import “Lietener.h”
int main(int argc, const char * argv[])
{
@autoreleasepool {
Lietener *l1 = [[Lietener alloc]init];
[l1 wantTolisten]; //监听者
BJbroadcast *bj = [[BJbroadcast alloc]init];
[bj broadcast]; //通知者
[[NSRunLoop currentRunLoop]run];
}
return 0;
}
为什么控制台什么都不输出呢?(xcode5.1.1)求大神解救啊。。。