改善系统的通知中心

改善系统的通知中心

iOS中的通知中心的实现实现机制是设计模式中的观察者.

在不进行任何修改的情况下,通知中心是这么使用的.

//
//  NormalViewController.m
//  NotificationCenter
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "NormalViewController.h"

@interface NormalViewController ()

@end

@implementation NormalViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 注册通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(notificationEvent:)
                                                 name:NSStringFromClass([self class])
                                               object:nil];

    // 发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:NSStringFromClass([self class])
                                                        object:nil
                                                      userInfo:@{@"name": @"YouXianMing"}];

    // 发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:NSStringFromClass([self class])
                                                        object:nil
                                                      userInfo:@{@"age": @"18"}];
}

- (void)notificationEvent:(id)sender
{
    NSNotification *tmp = (NSNotification *)sender;
    NSLog(@"%@", tmp.userInfo);
}

- (void)dealloc
{
    // 移除通知中心
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:NSStringFromClass([self class])
                                                  object:nil];
}

@end

没有对象的概念(都是通过类方法展示):

获取数据部分还有点诡异:

 

要将其修改到具备对象的概念,且使用起来更加人性化:)以下就是本人对齐进行的修改.

NSObject+NotificationCenter.h   +   NSObject+NotificationCenter.m

//
//  NSObject+NotificationCenter.h
//
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>

#ifndef SELF_CLASS_NAME
#define SELF_CLASS_NAME   [self className]
#endif

@interface NSObject (NotificationCenter)

@property (nonatomic, strong) NSString   *notificationName;  // 通知中心名字

// 发送通知消息
- (void)sendMessage:(NSDictionary *)obj toName:(NSString *)name;

// 注册通知中心
- (void)registerNotificationName:(NSString *)name selector:(SEL)selector;

// 移除通知中心
- (void)removeNotification:(NSString *)name;

// 发送消息的对象
- (id)messageObject;

// 消息名字
- (NSString *)messageName;

// 当前对象名字
+ (NSString *)ClassName;
- (NSString *)className;

@end
//
//  NSObject+NotificationCenter.m
//
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "NSObject+NotificationCenter.h"
#import <objc/runtime.h>

@implementation NSObject (NotificationCenter)

/* ================== 扩展了一个属性 ================== */
static char notificationNameFlag;
- (void)setNotificationName:(NSString *)notificationName
{
    objc_setAssociatedObject(self, ¬ificationNameFlag,
                             nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, ¬ificationNameFlag,
                             notificationName,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)notificationName
{
    return objc_getAssociatedObject(self, ¬ificationNameFlag);
}
/* ================== 扩展了一个属性 ================== */

- (void)sendMessage:(NSDictionary *)obj toName:(NSString *)name
{
    // 发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:name
                                                        object:nil
                                                      userInfo:obj];
}

- (void)registerNotificationName:(NSString *)name selector:(SEL)selector
{
    // 如果没有提供名字,则用这个类的名字来注册
    if (name == nil)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:selector
                                                     name:NSStringFromClass([self class])
                                                   object:nil];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:selector
                                                     name:name
                                                   object:nil];
    }
}

- (void)removeNotification:(NSString *)name
{
    // 如果没有提供名字,则用这个类的名字来移除(注意哦,此处需要与注册处的名字一致!)
    if (name == nil)
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:NSStringFromClass([self class])
                                                      object:nil];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:name
                                                      object:nil];
    }
}

+ (NSString *)ClassName
{
    // 返回类名
    return NSStringFromClass(self);
}

- (NSString *)className
{
    // 返回类名
    return NSStringFromClass([self class]);
}

- (id)messageObject
{
    // 消息实体内容
    if ([self isKindOfClass:[NSNotification class]])
    {
        NSNotification *tmp = (NSNotification *)self;
        return tmp.userInfo;
    }
    else
    {
        return nil;
    }
}

- (NSString *)messageName
{
    // 注册消息者的名字
    if ([self isKindOfClass:[NSNotification class]])
    {
        NSNotification *tmp = (NSNotification *)self;
        return tmp.name;
    }
    else
    {
        return nil;
    }
}

@end

使用时的代码如下:

//
//  RootViewController.m
//  NotificationCenter
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "NSObject+NotificationCenter.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 注册通知中心
    [self registerNotificationName:[self className]
                          selector:@selector(notificationEvent:)];

    // 对象A发送通知
    [@"A" sendMessage:@{@"name": @"YouXianMing"}
               toName:[self className]];

    // 对象B发送通知
    [@"B" sendMessage:@{@"age": @"18"}
               toName:[self className]];
}

- (void)notificationEvent:(id)sender
{
    // 获取到message
    id object = [sender messageObject];

    // 打印message
    NSLog(@"%@", object);
}

- (void)dealloc
{
    // 移除注册的通知中心
    [self removeNotification:[self className]];
}

@end

至少,我们减少了认知上面得差异,不需要你知道有通知中心这个东西存在了,取数据也有专门的方法直接获取.

其实,我还扩展了一个NSObject的一个属性,这个属性就是用来标示被注册通知中心名字的,以下展示的是高级用法.

创建Student的Model

//
//  Student.h
//  NotificationCenter
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject

@property (nonatomic, strong) NSString *name;

@end
//
//  Student.m
//  NotificationCenter
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "Student.h"
#import "NSObject+NotificationCenter.h"

@implementation Student

@synthesize name = _name;
- (void)setName:(NSString *)name
{
    _name = name;

    if (self.notificationName)
    {
        [self sendMessage:@{@"data": name} toName:self.notificationName];
    }
}
- (NSString *)name
{
    return _name;
}

@end

使用:

//
//  RootViewController.m
//  NotificationCenter
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "NSObject+NotificationCenter.h"
#import "Student.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 注册通知中心
    [self registerNotificationName:[self className]
                          selector:@selector(notificationEvent:)];

    Student *stu         = [Student new];
    stu.notificationName = [self className];
    stu.name             = @"YouXianMing";
}

- (void)notificationEvent:(id)sender
{
    // 获取到message
    id object = [sender messageObject];

    // 打印message
    NSLog(@"%@", object);
}

- (void)dealloc
{
    // 移除注册的通知中心
    [self removeNotification:[self className]];
}

@end

其实,这已经从"不记名字"的通知中心转变成了"记名"的通知中心了,使用起来也是非常简单的:)

时间: 2024-08-30 18:54:41

改善系统的通知中心的相关文章

MAC OS X 10.8如何重启通知中心

  MAC OS X 10.8系统中通知中心有时会出现无法访问.停止更新等问题,为了解决这些问题,重启通知中心是一个很好的方法.下面就和小编一起来看看具体的操作方法吧. 解决方法: 1. 通过右上方Spotlight搜索[活动监视器]并打开 2. 在[活动监视器]右上方搜索条中搜索"通知中心" 3. 选中搜索到的这个进程,然后点左芳芳红色"退出进程" 这样通知中心便会自动重启了,一般遇到的错乱问题也会随之解决. 以上就是解决通知中心重启的方法了,有遇到通知中心出现无

MAC OS X 10.8通知中心无法访问、停止更新怎么重启

  MAC OS X 10.8系统中通知中心有时会出现无法访问.停止更新等问题,为了解决这些问题,重启通知中心是一个很好的方法.下面就和小编一起来看看具体的操作方法吧. 解决方法: 1. 通过右上方Spotlight搜索[活动监视器]并打开 2. 在[活动监视器]右上方搜索条中搜索"通知中心" 3. 选中搜索到的这个进程,然后点左芳芳红色"退出进程" 这样通知中心便会自动重启了,一般遇到的错乱问题也会随之解决. 以上就是解决通知中心重启的方法了,有遇到通知中心出现无

重写通知中心类

重写通知中心类 笔者重新设计了通知中心类,功能完全与系统的通知中心一致,但有着比系统通知中心更优秀的地方: 1. 注册了通知中心不需要手动移除,如果注册对象自动释放了,在通知中心中注册的信息也会自动消失 2. 传递的参数可以是任何的对象,包括数组,字典等等一切对象 3. 基于NSObject的category扩展而来,使用非常的方便 所有的源码如下: CustumNotification.h // // CustumNotification.h // // http://home.cnblogs

Android 4.3通知中心功能强悍 有望整合第三方配件

&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp;   日前在Galaxy S4标准版和Google Play版上我们率先体验了Android 4.3系统版本,在界面上并未有太大的变化更多的后台和内部细节的调整优化. 不过外媒今天发现了新系统中通知中心变得更加的强大,为那些第三方配件提供了 更加广阔的大门.未来有望通过 深度整合为Google Glass或者智能手表等 外设提供更加快捷的操作体验.       在最

解析iOS系统的自带通知中心与勿扰模式功能

硅谷网讯 虽然目前智能手机在http://www.aliyun.com/zixun/aggregation/32533.html">我们的生活中扮演了非常重要的角色,无论是什么时候所有的提醒信息都能够第一时间通过智能手机接收, 但是有时过多的信息或者提醒反而会让我们的iPhone看起来有些乱七八糟,并且有时突然会在半夜发出提醒,影响我们的休息. 今天,就为大家介绍一下如果管理和使用我们手中的iPhone或iPad自带的通知中心及勿扰模式,还自己一个清静的世界. 通知中心 自带的通知中心和勿

Chrome 28 Beta版增加独立通知中心

  距离Chrome 27的稳定版本发布才两天时间,谷歌马不停蹄,继续宣布了Windows.Mac和Linux版本的Chrome 28 Beta的推出.该版本最引人注目的是独立通知中心功能的加入,众所周知,目前只有Windows版本才具有此功能. 谷歌方称:"我们将这些通知设计得既有花瓶的迷人外表,又有内在的实用性."来自app的通知以及扩展功能可以转化为文本显示或者图片的格式,还会在弹出窗口里直接包含一些动作功能. 这里给出一个例子,如下图所示: Chrome和Chrome操作系统同

Mac通知中心怎么用?

  有很多使用Mac的朋友可能忽略了Mac的一项非常好用的功能--"通知中心",这个平时不起眼的功能可以为我们提供日历.提醒事项.天气情况.股票信息.社交信息等信息的便捷查看,今天小编就来教大家如何设置Mac的通知中心方便我们使用. 1.首先在系统界面的右上角找到通知中心的开启按钮,如果是配备了苹果触控板的Mac用户也可以通过双指由触控板右侧划入打开通知中心. 2.通知中心默认为我们添加了部分显示的项目,我们可以自己手动添加或删减这些项目的显示,在通知中心的底部有"编辑&qu

Mac怎么把 iTunes 添加到通知中心?

  Mac怎么把 iTunes 添加到通知中心?当前 Mac OS X最新的 iTunes 支持通知中心的插件功能,可以让我们直接在通知中心里控制播放音乐,非常的方便.这里小编给大家简单介绍下具体的操作方法. 1.首先,要想在 Mac 上的通知中心实现 iTunes 控制功能,首先需要系统是 OS X 10.10 版本,另外还需要将你的 iTunes 升级到最新的 12.2.1 版本. 2.当我们升级好了 iTunes 应用以后,打开通知中心,在底部可以看到有一个"新项目"提示,点击它

Mac通知中心设置方法

  对于很多Mac户来说,通知中心是一个很不错的功能.不过,很多Mac的新用户可能还不太了解这个平时不起眼的功能.今天小编就来跟大家分享Mac通知中心设置方法. 1.首先在系统界面的右上角找到通知中心的开启按钮,如果是配备了苹果触控板的Mac用户也可以通过双指由触控板右侧划入打开通知中心. 2.通知中心默认为我们添加了部分显示的项目,我们可以自己手动添加或删减这些项目的显示,在通知中心的底部有"编辑"按钮. 3.如图所示左侧红色"-"号为删除该项目显示,右侧绿色&q