NSNotificationCenter 的详细说明

本文详细介绍了NSNotification和NSNotificationCenter的工作原理,包括如何发送、接收通知,以及如何利用userInfo传递额外信息。此外,还探讨了如何将对象注册为观察者以及如何处理通知。

-- NSNotification 和 NSNotificationCenter

Notification对象非常简单. 它就是poster要提供给observer的信息包裹. notification对象有两个重要的成员变量: 

name 和 object. 一般object都是指向poster(为了让observer在接受到notification时可以回调到poster)

所以,notification有两个方法

    - (NSString *)name

    - (id)object

NSNotificaitonCernter是架构的大脑了.它允许我们注册observer对象, 发送notification, 撤销observer对象注册

下面是它的一些常用方法

+ (NSNotificationCenter *)defaultCenter

返回notification center [类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]

- (void)addObserver:(id)anObserver

           selector:(SEL)aSelector

               name:(NSString *)notificationName

             object:(id)anObject

注册anObserver对象:接受名字为notificationName, 发送者为anObject的notification. 当anObject发送名字

为notificationName的notification时, 将会调用anObserver的aSelector方法,参数为该notification 如图14.1

. 如果notificationName为nil. 那么notification center将anObject发送的所有notification转发给observer

. 如果anObject为nil.那么notification center将所有名字为notificationName的notification转发给observer

 

- (void)postNotification:(NSNotification *)notification

发送notification至notification center 如图14.2

 

- (void)postNotificationName:(NSString *)aName

                      object:(id)anObject

创建并发送一个notification

 

- (void)removeObserver:(id)observer

移除observer

-- 发送一个Notification --

发送notification是其中最简单的步骤了,所以我们从它开始实现.当我们接收到changeBackgroundColor:消息时, 

PreferenceController对象发送一个notification.

 

我们将notification命名为@"BNRColorChanged" ,我们使用一个全局常量来指定.(有经验的程序员会使用一个前缀,

这样避免和其他组件定义的notification混淆)打开PreferenceController.h 添加下面的的外部申明

extern NSString * const BNRColorChangedNotification;

在PreferenceController.m中定义常量

NSString * const BNRColorChangedNotification = @"BNRColorChanged";

在PreferenceController.m修改changeBackgroundColor:方法

- (IBAction)changeBackgroundColor:(id)sender

{

    NSColor *color = [colorWell color];

    NSData *colorAsData =

                  [NSKeyedArchiver archivedDataWithRootObject:color];

    [[NSUserDefaults standardUserDefaults] setObject:colorAsData

                                          forKey:BNRTableBgColorKey];

 

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    NSLog(@"Sending notification");

    [nc postNotificationName:BNRColorChangedNotification object:self];

}

-- 注册成为Observer --

要注册一个observer, 我们必须提供几个要数: 要成为observer的对象;所感兴趣的notification的名字;

当notification发送时要调用的方法. 我们也可以指定要关注莫个对象的notification.(比如说,我们需要

关注莫个特定的window的resize的notification)

 

编辑MyDocument类的init方法

- (id)init

{

    if (![super init])

        return nil;

 

    employees = [[NSMutableArray alloc] init];

 

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    [nc addObserver:self

           selector:@selector(handleColorChange:)

               name:BNRColorChangedNotification

             object:nil];

    NSLog(@"Registered with notification center");

    return self;

}

同时在dealloc方法,将MyDocument从notification center中移除

- (void)dealloc

{

    [self setEmployees:nil];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    [nc removeObserver:self];

    [super dealloc];

}

-- 处理Notification --

当一个notification发生时, handleColorChange:方法将被调用. 目前我们在方法中简单的打印一些log.

- (void)handleColorChange:(NSNotification *)note

{

    NSLog(@"Received notification: %@", note);

}

编译运行程序,看到了我们想要的log了吧

-- userInfo Dictionary --

notification对象的object变量是poster,如果我们想要notification对象传递更多的信息,

我们可以使用user info dictionary. 每个notification对象有一个变量叫 userInfo, 它是一个NSDictionary对象,

用来存放用户希望随着notification一起传递到observer的其它信息. MyDocument将使用它来得到要改变的color.

在PreferenceController.m添加userInfo

- (IBAction)changeBackgroundColor:(id)sender

{

    NSColor *color = [sender color];

    NSData *colorAsData;

    colorAsData = [NSKeyedArchiver archivedDataWithRootObject:color];

    [[NSUserDefaults standardUserDefaults] setObject:colorAsData

                                          forKey:BNRTableBgColorKey];

 

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    NSLog(@"Sending notification");

    NSDictionary *d = [NSDictionary dictionaryWithObject:color

                                                  forKey:@"color"];

    [nc postNotificationName:BNRColorChangedNotification

                      object:self

                    userInfo:d];

}

在MyDocument.m,从userInfo中读取到color

- (void)handleColorChange:(NSNotification *)note

{

    NSLog(@"Received notification: %@", note);

    NSColor *color = [[note userInfo] objectForKey:@"color"];

    [tableView setBackgroundColor:color];

}

打开几个窗口,并改变背景颜色,现在,那些打开的窗口的背景颜色立马就变了.

-- 思考 --

通常当你将自己的一个对象设置为cocoa某个标准对象的delegate的时候,你同时或许也对该标准对象的notification感兴趣. 

例如,我们实现一个window的delegate来处理 windowShouldClose: , 我们也许会对 NSWindowDidResizeNotification

这样的notification感兴趣.

如果一个cocoa标准对象有一个delegate,同时它也发送notification的话, cocoa对象会自动将它的delegate对象注册

成为observer来接受接受自己的notification. 如果我们实现了一个delegate,那么delegate[也就是我们的对象]

要怎样声明来接受notification呢?[方法的名字是什么?]

 

方法名字其实很简单: 以notification名字为基准, 先将NS前缀去掉,接着将第一个字母改为小写. 在将后面的Notification去掉,

然后加个冒号:. 例如,为了能接受到window的NSWindowDidResizeNotification, delegate可以实现方法:

- (void)windowDidResize:(NSNotification *)aNotification

 

当window改变大小时,这个方法将自动调用. 对于NSWindow,我们可以在.h或是帮助文档中找到类似的notification 

来实现notification方法.

-- 挑战 --

当程序不再是active状态是,让程序发出beep. 当unactive时,NSApplication会发送

NSApplicationDidResignActiveNotification的notificaiton. 而我们的AppController是NSApplication的delegate.   

函数NSBeep()可以用来发出beep声音

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值