前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通知 - NSNotificationCenter

通知 - NSNotificationCenter

作者头像
tandaxia
发布2018-09-27 12:30:05
1.1K0
发布2018-09-27 12:30:05
举报
文章被收录于专栏:谈补锅谈补锅

1、每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信;

2、任何一个对象都可以向通知中心发布通知(NSNotification), 描述自己在做什么。其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知。

3、通知是多对多的关系:

4、一个完整的通知一般包含3个属性:

    1)- (NSString *)name;   //通知的名称

    2)- (id)object;   //通知发布者(是谁要发布通知)

    3)- (NSDictionary *)userInfo;   //一些额外的信息(通知发布者传递给通知接收者的信息内容)

5、初始化一个通知(NSNotification)对象的构造函数:

     1) + (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;

     2) + (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

     3) - (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;

6、注册通知监听器:

  通知中心提供了方法类注册一个坚挺通知的监听器(Observer):

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

-->observer: 监听器。即谁要接收这个通知

-->aSelector: 收到通知后,回调监听器的这个方法,并且把通知对象做为参数传入

--> aName: 通知的名称。如果为nil, 那么无论通知的名称是什么,监听器都能收到这个通知

--> anObject: 通知发布者。如果anObject和aName都为nil, 监听器都收到所有的通知

7、代码示例:

创建一个通知发布者类NewsCompany

NewsCompany.h文件代码:

代码语言:javascript
复制
#import <Foundation/Foundation.h>

@interface NewsCompany : NSObject

+ (instancetype)newsCompanyWithName:(NSString *)name; //类构造方法
@property (nonatomic, copy) NSString *name;  //消息发布者名称

@end

NewsCompany.m文件代码:

代码语言:javascript
复制
#import "NewsCompany.h"

@implementation NewsCompany

+ (instancetype)newsCompanyWithName:(NSString *)name{
    NewsCompany *company = [[self alloc] init];
    company.name = name;
    return company;
}

@end

创建一个通知接受者类(监听者)Person

Person.h文件代码:

代码语言:javascript
复制
#import <Foundation/Foundation.h>

@interface Person : NSObject

+ (instancetype)personWithName:(NSString *)name; //类实例方法
@property (nonatomic, copy) NSString *name; //监听对象的名称

//监听通知的方法
- (void)MonitorMessage:(NSNotification *)notification;

@end

Person.m文件代码:

代码语言:javascript
复制
#import "Person.h"

@implementation Person

+ (instancetype)personWithName:(NSString *)name
{
    Person *per = [[self alloc] init];
    per.name = name;
    return per;
}

- (void)MonitorMessage:(NSNotification *)notification
{
    NSLog(@"监听者: %@,通知名称:%@, 其他信息:%@", self.name, notification.name, notification.userInfo);
}

//就算在arc机制下,还是要移除当前监听者的所有监听行为
- (void)dealloc
{
    NSLog(@"移除当前监听者: %@", self.name);
    [[NSNotificationCenter defaultCenter] removeObserver:nil];   //移除当前监听者的所有监听行为
}
@end

main方法里执行代码为:

代码语言:javascript
复制
#import <Foundation/Foundation.h>
#import "NewsCompany.h"
#import "Person.h"

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

        //创建两个发布通知者对象
        NewsCompany *com1 = [NewsCompany newsCompanyWithName:@"腾讯"];
        NewsCompany *com2 = [NewsCompany newsCompanyWithName:@"新浪"];
        
        //创建五个监听者对象
        Person *per1 = [Person personWithName:@"谭大"];
        Person *per2 = [Person personWithName:@"毛二"];
        Person *per3 = [Person personWithName:@"张三"];
        Person *per4 = [Person personWithName:@"李四"];
        Person *per5 = [Person personWithName:@"王五"];
        
        //取得通知中心对象
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        
        //设置两个通知的名称
        NSString *notice1 = @"一路一带";
        NSString *notice2 = @"亚投行";
        
        //步骤1、先注册通知的监听者
        /* 
         - (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
         参数说明:
         observer: 要注册的监听者对象
         aSelector: 监听者的监听方法
         aName: 通知名称,为nil表示监听者监听通知中心的所有通知
         anObject: 通知发布者对象,为nil表示监听者监听所有的通知发布者
         如果通知发布者没有发布某个通知,则注册为该通知发布者的监听者将监听不到信息
        */
        [center addObserver:per1 selector:@selector(MonitorMessage:) name:nil object:nil];
        [center addObserver:per2 selector:@selector(MonitorMessage:) name:notice1 object:nil];
        [center addObserver:per3 selector:@selector(MonitorMessage:) name:nil object:com1];
        [center addObserver:per4 selector:@selector(MonitorMessage:) name:notice1 object:com1];
        
        //反列:per5对象注册的通知发布者com1没有发布通知notice2, 则per5监听者对象将监听不到通知
        [center addObserver:per5 selector:@selector(MonitorMessage:) name:notice2 object:com1];
        
        //步骤2、通知对象再发布通知
        [center postNotificationName:notice1 object:com1 userInfo:@{@"otherInfo": @"1111111"}];
        [center postNotificationName:notice2 object:com2 userInfo:@{@"otherInfo": @"2222222"}];
        [center postNotificationName:notice1 object:com2 userInfo:@{@"otherInfo": @"3333333"}];
    }
    return 0;
}

打印结果为:

代码语言:javascript
复制
监听者: 谭大,通知名称:一路一带, 其他信息:{ otherInfo = 1111111; }
监听者: 毛二,通知名称:一路一带, 其他信息:{ otherInfo = 1111111; }
监听者: 张三,通知名称:一路一带, 其他信息:{ otherInfo = 1111111; }
监听者: 李四,通知名称:一路一带, 其他信息:{ otherInfo = 1111111; }
监听者: 谭大,通知名称:亚投行, 其他信息:{ otherInfo = 2222222; }
监听者: 谭大,通知名称:一路一带, 其他信息:{ otherInfo = 3333333; }
监听者: 毛二,通知名称:一路一带, 其他信息:{ otherInfo = 3333333; }
移除当前监听者: 王五
移除当前监听者: 李四
移除当前监听者: 张三
移除当前监听者: 毛二
移除当前监听者: 谭大

说明:1)、当注册监听者时,通知名称和通知发布者为nil, 则默认监听者注册为通知中心的所有通知发布者的通知;

         2)、如果注册监听者时,通知发布者没有发布该通知,则监听者监听不到该通知

UIDevice通知

1)、UIDevice类提供了一个单列对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电池电量值(batteryLevel)、电池状态(batteryState)、设备的类型(model, 比如iPod、iPhone等) 、设备的系统(systemVersion)

2)、通过[UIDevice currentDevice]可以获取这个单列对象

3)、UIDevice对象会不间断的发布一些通知,下列是UIDevice对象所发布通知的名称常量:

     UIDeviceOrientationDidChangeNotification  //设备旋转

     UIDeviceBatteryStateDidChangeNotification //电池状态改变

     UIDeviceBatteryLevelDidChangeNotification  //电池电量改变

     UIDeviceProximityStateDidChangeNotification  //近距离传感器( 比如设备贴近了使用者的脸部)

键盘通知:

键盘状态改变的时候,系统会发出一些特定的通知

UIKeyboardWillShowNotification   //键盘即将显示

UIKeyboardDidShowNotification  //键盘显示完毕

UIKeyboardWillHideNotification   //键盘即将隐藏

UIKeyboardDidHideNotification   //键盘隐藏完毕

UIKeyboardWillChangeFrameNotification   //键盘的位置尺寸即将发生改变

UIKeyboardDidChangeFrameNotification  //键盘的位置尺寸改变完毕

9、设置文本框的边距,假设文本框变量名为inputV

     inputV.leftView = [UIView alloc] initWithFrame:CGRectMake(0,  0,  10,  0)];//设置边距

     inputV.leftViewMode = UITextFieldViewModeAlways;    //设置左边距模式

10、通知示例,监听viewController上的文本框变化

代码语言:javascript
复制
 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     // Do any additional setup after loading the view, typically from a nib.
 4     
 5     //监听开始编辑文本框
 6     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tanBeginEditText) name:UITextFieldTextDidBeginEditingNotification object:nil];
 7     
 8     //监听改变文本框内容
 9     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tanChangeText) name:UITextFieldTextDidChangeNotification object:nil];
10     
11     //监听结束文本框编辑
12     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tanEndEditText) name:UITextFieldTextDidEndEditingNotification object:nil]; 
13 }
14 
15 //监听开始编辑文本框
16 - (void)tanBeginEditText{
17     NSLog(@"开始编辑");
18 }
19 
20 //监听文本框编辑过程
21 - (void)tanChangeText{
22     NSLog(@"正在输入文字....");
23 }
24 
25 //监听文本框结束
26 - (void)tanEndEditText{
27     NSLog(@"结束编辑");
28 }

运行打印结果:

代码语言:javascript
复制
开始编辑
正在输入文字....
正在输入文字....
正在输入文字....
结束编辑
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-05-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档