BjBroadcast.h

//
//  BjBroadcast.h
//  NotificationDemo
//
//  Created by valenty on 14-5-26.
//  Copyright (c) 2014年 valenty. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BjBroadcast : NSObject

-(void) broadcast;
-(void) broadcastLooper;

@end

BjBroadcast.m

//
//  BjBroadcast.m
//  NotificationDemo
//
//  Created by valenty on 14-5-26.
//  Copyright (c) 2014年 valenty. All rights reserved.
//

#import "BjBroadcast.h"

@implementation BjBroadcast


//定时器,定时发
-(void) broadcastLooper{
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(broadcast) userInfo:nil repeats:YES];
     //启动一个定时器,循环改善广播
}

-(void) broadcast{
    NSLog(@"start");
    // 1:取得通知中心
    NSNotificationCenter *nc=[NSNotificationCenter defaultCenter];
    //2.发送广播
    static int i;
    NSString *count=[NSString stringWithFormat:@"bcast %d",i++];
    NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"Bj broadcast",@"Name",count,@"Value" , nil];
    //dict消息内容
    //2.改善广播
    [nc postNotificationName:@"Bj broadcast" object:self userInfo:dict];
    //param1广播的频段
    
}

@end

Listener.h

//
//  Listener.h
//  NotificationDemo
//
//  Created by valenty on 14-5-26.
//  Copyright (c) 2014年 valenty. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Listener : NSObject

-(void) wantToListen;

@end

Listener.m

//
//  Listener.m
//  NotificationDemo
//
//  Created by valenty on 14-5-26.
//  Copyright (c) 2014年 valenty. All rights reserved.
//

#import "Listener.h"

@implementation Listener

-(void) wantToListen{
    //1注册,
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recvBcast:) name:@"Bj broadcast" object:nil];
    //param1,param2 这两个参数是只要有BJBroadcast这个广播就调用【self recvBcast:】
    //param4,参数一般都设为nil.
    //2要真正的接收广播数据
    
}
-(void) recvBcast:(NSNotification *)notify{
    //notify就是具体的广播消息
    NSString *name=notify.name;
 

    NSLog(@"notify is %@",[notify.userInfo objectForKey:@"Name" ]);
}

@end

 

posted on 2014-05-26 22:41  .NET每天一小步  阅读(285)  评论(0)    收藏  举报