您的位置:首页 > 其它

[Cocoa]_[初级]_[NSNotificationCenter 消息中心在程序中的具体应用]

2015-12-23 19:28 302 查看
1.NSNotificationCenter是整个消息操作的首脑 。它允许你注册观察者对象,发布通知到通告板上,和注销观察对象。

2.消息中心机制流程图:



主要是对观察者对象进行注册,注册之后NSNotificationCenter对所有观察者对象进行统一管理,如果观察者要执行方法,发送一条消息给消息中心,消息中心监听到这个消息之后,就会把你感兴趣的方法显示到通告板上,以方便用户预览。其中[NSNotificationCenter defaultCenter]这个是消息中心对象是全局的,处理消息的方式是同步模式。

3.场景:我们以切换视图为例,使用消息中心来切换视图,可以避免视图之间的相互引用。所有视图的切换命令都有消息中心来监听管理,只需要给消息中心发送一个命令请求,消息中心监听到这个请求命令之后就会执行我们要操作的方法。

4.具体实现步骤:

1)创建一个消息中心

 NSNotificationCenter *nc =[NSNotificationCenter defaultCenter];

2)注册观察者

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

3)发送消息

- (void)postNotificationName:(NSString *)aName object:(NSString *)anObject;

4)注销观察者

- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
下面看具体例子说明

WindowNotification.h

#import <Foundation/Foundation.h>

extern NSString * const kWindowMessageSwitchToMainView;
extern NSString * const kWindowMessageSwitchToFirstView ;
extern NSString * const kWindowMessageSwitchToSecondView ;
WindowNotification.m

#import "WindowNotification.h"

//定义各个视图的观察者名称
NSString * const kWindowMessageSwitchToMainView = @"kWindowMessageSwitchToMainView";
NSString * const kWindowMessageSwitchToFirstView = @"kWindowMessageSwitchToFirstView";
NSString * const kWindowMessageSwitchToSecondView = @"kWindowMessageSwitchToSecondView";

FirstViewController.m
#import "FirstViewController.h"
#import "WindowNotification.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(IBAction)onBackToHomePager:(id)sender
{
//返回到主视图
NSNotificationCenter *nc =[NSNotificationCenter defaultCenter];
[nc postNotificationName:kWindowMessageSwitchToMainView object:nil];
}

@end

SecondViewController.m
#import "SecondViewController.h"
#import "WindowNotification.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}

-(IBAction)onBackToHomePager:(id)sender
{
//返回到主视图
NSNotificationCenter *nc =[NSNotificationCenter  defaultCenter];
[nc postNotificationName:kWindowMessageSwitchToMainView object:nil];
}


MainDelegate.h
#import <Foundation/Foundation.h>
@class FirstViewController;
@class SecondViewController;

@interface MainDelegate : NSObject
{
IBOutlet NSView *mainView;
IBOutlet NSScrollView* parentView;

FirstViewController* firstViewController;//视图1
SecondViewController* secondViewController;//视图2
}

-(IBAction)onfirstView:(id)sender;
-(IBAction)onSecondView:(id)sender;
-(IBAction)onRemoveObserver:(id)sender;

-(void) doSwitchMainWindow;
-(void) doSwitchFirstViewController;
-(void) doSwitchSecondViewController;

@end

MainDelegate.m
#import "MainDelegate.h"
#import "WindowNotification.h"

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation MainDelegate

-(void)awakeFromNib
{
firstViewController =[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
secondViewController =[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
//创建一个消息中心对象
NSNotificationCenter *nc =[NSNotificationCenter  defaultCenter];
//注册观察者对象
[nc addObserver:self selector:@selector(doSwitchMainWindow) name:kWindowMessageSwitchToMainView object:nil];
[nc addObserver:self selector:@selector(doSwitchFirstViewController) name:kWindowMessageSwitchToFirstView object:nil];
[nc addObserver:self selector:@selector(doSwitchSecondViewController) name:kWindowMessageSwitchToSecondView object:nil];
}

-(IBAction)onfirstView:(id)sender
{
//发送消息切换视图1
NSNotificationCenter *nc =[NSNotificationCenter  defaultCenter];
[nc postNotificationName:kWindowMessageSwitchToFirstView object:nil];
}
-(IBAction)onSecondView:(id)sender
{
//发送消息切换视图2
NSNotificationCenter *nc =[NSNotificationCenter  defaultCenter];
[nc postNotificationName:kWindowMessageSwitchToSecondView object:nil];
}
-(IBAction)onRemoveObserver:(id)sender
{
//注销所有观察者
NSNotificationCenter *nc =[NSNotificationCenter  defaultCenter];
[nc removeObserver:self name:kWindowMessageSwitchToFirstView object:nil];
[nc removeObserver:self name:kWindowMessageSwitchToSecondView object:nil];

}

-(void) doSwitchMainWindow
{
[mainView retain];
[parentView setDocumentView:mainView];
}
-(void) doSwitchFirstViewController
{
[[parentView documentView] retain];
[parentView setDocumentView:firstViewController.view];
}
-(void) doSwitchSecondViewController
{
[[parentView documentView] retain];
[parentView setDocumentView:secondViewController.view];
}

@end


运行结果:



点击“FirstView”控件,显示视图1



点击“SecondView”控件,显示视图2



点击“Back to homepage”控件,返回主视图.

如果点击主视图中的“Remove all Observers”控件,再次点击“FirstView”控件和“SecondView”控件,将会不会再次切换视图,因为观察者对象已经中消息中心中注销了。

项目代码网址:

http://download.csdn.net/detail/moqj_123/9374350    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息