1. 通知中心概述
通知中心实际上是在程序内部提供了消息广播的一种机制。通知中心不能在进程间进行通信。实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象。通知中心是基于观察者模式的,它允许注册、删除观察者。
一个NSNotificationCenter可以有许多的通知消息NSNotification,对于每一个NSNotification可以有很多的观察者Observer来接收通知。
2. 通知中心两个重要的类
NSNotificationCenter:这是iOS中通知中心的灵魂,由该类实现了观察者模式,并给开发者提供了诸如注册、删除观察者的接口,我们可以通过一个单例来获得它的实例(注,一个程序内部只有一个NSNotificationCenter实例对象)。
NSNotification:这是消息携带的载体,通过它,可以把消息内容传递给观察者。其中:name对应消息名称标示。object一般是发送者本身、dictionary则是传递的消息内容。
3. 通知中心如何使用
通过下图,我们可以看出,通知中心的使用可以分为4个步骤。
这里需要额外提一点的是:发送消息不仅仅可以有用户发起,也可以是系统发起。
当我们注册了某个消息的观察者后,如果有了对应的消息,则观察者会收到相应的消息,并展开处理。这里需要注意的是,当使用完消息之后,不想在接收到消息,则需要把观察者移除,否则会出现错误。
注册通知:即要在什么地方接受消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (mytest:) name:@" postData " object:nil];
参数介绍:
addObserver:观察者,即在什么地方接收通知;
selector:收到通知后调用何种方法,即回调函数;
name:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
发送通知:调用观察者处的方法。
[[NSNotificationCenter defaultCenter] postNotificationName:@" postData " object:searchFriendArray];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:avc1];
self.window.rootViewController = nav;
广播者:
#import <UIKit/UIKit.h>
@interface NotificationViewController : UIViewController
@end
#import "NotificationViewController.h"
@interface NotificationViewController ()
@end
@implementation NotificationViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"广播";
UIButton *redButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
redButton.frame = CGRectMake(100, 20, 100, 40);
[redButton setTitle:@"蓝色" forState:UIControlStateNormal];
[redButton addTarget:self action:@selector(redButtonClick:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:blueColor];
UIButton *blackButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
blackButton.frame = CGRectMake(100, 80, 100, 40);
[blackButton setTitle:@"绿色" forState:UIControlStateNormal];
[blackButton addTarget:self action:@selector(blackButtonClick:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:greenColor];
//一下一行用于接收广播,对发广播无影响。
//首先将自己注册为广播的听众addObserver:self,要听的广播的内容@selector(recvBcast:),选定广播的频道name:@"ChangeTheme"
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(recvBcast:) name:@"ChangeTheme" object:nil];
}
- (void) redButtonClick:(id)arg {//-------发广播
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// 取得ios系统唯一的全局的广播站 通知中心
NSString *name = @"红色经典";
UIColor *c = [UIColor redColor];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
name, @"ThemeName",
c, @"ThemeColor", nil];
//设置广播内容
[nc postNotificationName:@"ChangeTheme" object:self userInfo:dict];
//将内容封装到广播中 给ios系统发送广播
// ChangeTheme频道
}
// 原理同上,广播的主题相同,但是内容不同。
- (void) blackButtonClick:(id)arg {
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
// 取得ios系统唯一的全局的广播站 通知中心
NSString *name = @"黑色经典";
UIColor *c = [UIColor blackColor];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
name, @"ThemeName",
c, @"ThemeColor", nil];
[nc postNotificationName:@"ChangeTheme" object:self userInfo:dict];
}
//可以不听自己发的广播
- (void) recvBcast:(NSNotification *)notify {//该函数用于取得广播内容
NSDictionary *dict = [notify userInfo];
// 取得广播内容
NSString *name = [dict objectForKey:@"ThemeName"];
UIColor *c = [dict objectForKey:@"ThemeColor"];
self.title = name;
self.view.backgroundColor = c;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
听众1:
#import <UIKit/UIKit.h>
@interface AudienceViewController1 : UIViewController
@end
#import "AudienceViewController1.h"
#import "AudienceViewController2.h"
@interface AudienceViewController1 ()
@end
@implementation AudienceViewController1
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"听众1";
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(100, 100, 100, 40);
[b addTarget:self action:@selector(buttonClick:)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:b];
// 我们喜欢听ChangeTheme的广播
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// 注册成为广播站ChangeTheme频道的听众
[nc addObserver:self selector:@selector(recvBcast:)name:@"ChangeTheme" object:nil];
// 成为听众一旦有广播就来调用self recvBcast:函数
}
// 这个函数是系统自动来调用
// ios系统接收到ChangeTheme广播就会来自动调用
// notify就是广播的所有内容
- (void) recvBcast:(NSNotification *)notify {
static int index;
NSLog(@"recv bcast %d", index++);
NSDictionary *dict = [notify userInfo];
// 取得广播内容
NSString *name = [dict objectForKey:@"ThemeName"];
UIColor *c = [dict objectForKey:@"ThemeColor"];
self.title = name;
self.view.backgroundColor = c;
}
- (void) buttonClick:(id)arg {
AudienceViewController2 *avc2 = [[AudienceViewController2 alloc] init];
[self.navigationController pushViewController:avc2 animated:YES];
//[AudienceViewController2 release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
听众2:
#import <UIKit/UIKit.h>
@interface AudienceViewController2 : UIViewController
@end
#import "AudienceViewController2.h"
#import "NotificationViewController.h"
@interface AudienceViewController2 ()
@end
@implementation AudienceViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"听众二";
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(100, 200, 100, 40);
[b addTarget:self action:@selector(buttonClick:)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:b];
//注册
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(recvBcast:) name:@"ChangeTheme" object:nil];
}
- (void) buttonClick:(id)arg {
NotificationViewController *nvc = [[NotificationViewController alloc] init];
[self.navigationController pushViewController:nvc animated:YES];
//[AudienceViewController2 release];
}
- (void) recvBcast:(id) arg {
NSNotification *notify = (NSNotification *)arg;
// 取得NSNotification广播内容
NSDictionary *dict = notify.userInfo;
NSString *name = [dict objectForKey:@"ThemeName"];
UIColor *c = [dict objectForKey:@"ThemeColor"];
self.title = name;
self.view.backgroundColor = c;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
这样,当广播者发送一个广播,广播的内容就是改变主题和改变背景颜色。
听众注册后,选定频道,就收听到广播的内容。然后就按照广播的内容去改变主题和背景颜色。
一般在应用的设置里用的比较多。