objective 消息中心的处理

来源:互联网 发布:433m模块stc单片机解码 编辑:程序博客网 时间:2024/05/16 11:14


先注册消息

        //接收消息(被攻击)
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(IS_attacked:)
                                                     name:@"bird_attack_pig"
                                                   object:nil];
        
        //接收消息(攻击)
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(IS_attack:)
                                                     name:@"pig_attack_bird"
                                                   object:nil];
        
        //接收消息(武器升级)
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(WEAPON_up:)
                                                     name:@"weapon_up"
                                                   object:nil];
        //linbo code
        //pig_bossdart 发出武器
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(pig_bossdart:)
                                                     name:@"pig_bossdart"
                                                   object:nil];

注册后当有消息发过来,自动会调用selector 里的方法,name 为对应的消息名


下面是一个上面用的事调用方法

-(void)pig_bossdart:(NSNotification *)sender
{
    NSDictionary *ns=[sender object];
    NSNumber *level= [ns objectForKey:@"level"];
    int a = [level intValue];
    NSNumber *direction= [ns objectForKey:@"direction"];
    int b=[direction intValue];
    [pig bossdart_attack:a direction:b];
    [pig setDelegate:self];
}


这里的sender  其实就是在其它地方发送消息传过来的参数,


发送消息


  //调用pig的动做
    NSNumber *temp_level;
    
    NSNumber *temp_direction;
    temp_level =[NSNumber numberWithInt:level1];
    temp_direction=[NSNumber numberWithInt:direction1];
    //    
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    [dict setObject:sender forKey:@"bossdart"];
    [dict setObject:temp_level forKey:@"level"];
    [dict setObject:temp_direction forKey:@"direction"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pig_bossdart" object:dict];

pig_bossdart 为消息名,,,dict 为参数









原创粉丝点击