OC中的通知

//
//  ViewController.m
//  OC-08_03
//
//  Created by Ibokan on 15/12/18.
//  Copyright © 2015年 ibokan. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "Weather.h"
#import "PhoneUser.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /*
       通知  NSNotification
     通过学习‘KVO’,我们发现‘KVO’是一种简单地观察者模式,涉及到两个对象,分别是观察者 和 被观察者,这种方式实质有很大的局限性,那么OC的‘Foundation’框架为开发者提供了新的一种观察者设计模式,既'通知'
     
     通知,是一种发送给一个或者多个观察者,用来通知其在程序中发生了某个事件的消息,COcoa中的通知机制遵循的是一种广播模式。它是一种程序中时间的发起者或者处理者和其他想要知道该事件的对象沟通的一种方式。消息的接收者,也就是观察者相应该事件改编自己的UI,行为或状态。
     
     在OC中,使用我们‘NSNotification’类来表示一个通知
     
     
     */
    //初始化一个NSNotification的实例对象
    NSNotification *notification1 = [NSNotification notificationWithName:@"通知名称1" object:self];
    NSNotification *notification2 = [NSNotification notificationWithName:@"通知名称2" object:self userInfo:@{@"content":@"Hello world"}];
    /*
     其中
     1.name:通知的名称,最好英文,用来识别通知对象
     2.object:表示通知的发起人
     3.userInfo:表示通知的内容
     */
    
    //这个通知就像现实中的邮件,邮件都需要邮局发送的接收人,中OC中一样,‘Foundation’框架定义了一个单例,通知中,NSNotificationCenter来统一发送通知的实例对象给观察者
    
    //通知中心  单例类:拿到通知中心的单例
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    Student *student = [Student new];
    
    //通知中心发送消息
    [center postNotification:notification2];
    
    /*
     建立通知发送机制,步骤如下:
     1.注册相关监听者,并实现监听到通知时的方法
     2.在需要的时候,被监听的对象去通知中心发送通知
     3.在‘dealloc’方法中,移除通知
     */
    Weather *weather = [Weather new];
    PhoneUser *phoneUser = [PhoneUser new];
    
    [weather sendMessage];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

创建一个Student类

//
//  Student.m
//  OC-08_03
//
//  Created by Ibokan on 15/12/18.
//  Copyright © 2015年 ibokan. All rights reserved.
//

#import "Student.h"

@implementation Student

-(id)init
{
    if (self = [super init]) {
        //注册监听者
        //到通知中心去让自己成为某个通知的监听对象
        /*
         1.要去接收通知的对象
         2.接收通知响应的方法
         3.接收通知的名称
         4.发起通知的对象,一般我们不需要知道,把它写成nil
         */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"通知名称2" object:nil];
    }
    return self;

}

//监听到通知后回调的方法
-(void)notificationAction:(NSNotification *)notification
{
    NSLog(@"Student 收到 %@",notification.userInfo);

}

-(void)dealloc
{
    //移除某个通知的监听者
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"通知名称2" object:nil];
    
    //移除所有通知的监听者
//    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

@end

创建一个Weather类

//
//  Weather.m
//  OC-08_03
//
//  Created by Ibokan on 15/12/18.
//  Copyright © 2015年 ibokan. All rights reserved.
//

#import "Weather.h"

@implementation Weather

-(void)sendMessage
{
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:WeatherAndPhoneUser object:self userInfo:@{@"weather":@"sunny"}];

}
@end

创建一个PhoneUser类

//
//  PhoneUser.m
//  OC-08_03
//
//  Created by Ibokan on 15/12/18.
//  Copyright © 2015年 ibokan. All rights reserved.
//

#import "PhoneUser.h"

@implementation PhoneUser

-(id)init
{
    if (self = [super init])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"weatherAndPhoneUser" object:nil];
    }
    return self;
}
//收到通知回调的方法
-(void)notificationAction:(NSNotification *)notification
{
    NSDictionary *dictionary = notification.userInfo;
    NSLog(@"今天的天气:%@",dictionary[@"weather"]);

}

//移除监听者
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"weatherAndPhoneUser" object:nil];

}
@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值