OC-通知中心应用

main.m



#import <Foundation/Foundation.h>

#import "Person.h"

#import "Goodperson.h"

int main(int argc, const char * argv[])

{

    @autoreleasepool

    {

        Person *p1=[Person new];

        Goodperson *g1=[Goodperson new];

        

        [p1 selectPerson];

        

    }

    return 0;

}


Person.m

#import "Person.h"


@implementation Person


- (void)selectPerson

{

    NSLog(@"挑选人物中.......");

    //我要做什么事情呢?我准备选人,然后把这个消息发出去

    //怎么发出去呢?通过通知中心发出去

    //1.获取通知中心

    NSNotificationCenter *center=[NSNotificationCenter defaultCenter];

    

    //2.发送通知

    

    [center postNotificationName:@"selectPerson" object:@{@"selecting":@"IOS"} userInfo:@{@"type":@"man"}];

}


@end




Goodperson.m


#import "Goodperson.h"


@implementation Goodperson

- (instancetype)init  //初始化 方法

{

    self = [super init];

    if (self) {

        //3.要接收通知对象

        //怎么接收?创建通知中心

        NSNotificationCenter *center=[NSNotificationCenter defaultCenter];

        [center addObserver:self selector:@selector(receiveNews:) name:@"selectPerson" object:nil];

        

        

    }

    return self;

}

//收到通知后,系统会把通知信息给你.

//收到之后要干什么?

//4.收到之后,执行你的方法

- (void)receiveNews:(NSNotification *)news

{

    NSLog(@"通知内容:%@",[news object]);

    NSLog(@"通知内容时附带的信息:%@",[ news userInfo]);

    NSLog(@"我已经在招人了");

}

//5.对象销毁前,从通知中心移除观察者

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"selectPerson" object:nil];

}






@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值