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”控件,將會不會再次切換視圖,因爲觀察者對象已經中消息中心中註銷了。
項目代碼網址: