通知模式:一个对象能够给其他任意数量的对象广播信息。对象之间可以没有耦合关系。
NSNotification(通知),封装了要广播的信息。 NSNotificationCenter(通知中⼼心),管理注册接收消息对象,广播消息。 observer(观察者),需要监测广播信息的对象,即接收信息的对象。
接收信息对象在通知中心进行注册,包括:信息名称、接收信息时的处理方法。
对象通过通知中心广播信息,包括:信息名称、信息内容。已经注册过的对象如果不需要接收信息时,在通知中心注销。
通知的核心代码大致三句:包括注册、注销和发送消息。
注册: [[NSNotificationCenter defaultCenter] addObserver:注册对象 selector:@selector(方法名) name:信息名称 object:nil]
注销: [[NSNotificationCenter defaultCenter] removeObserver:注销对象 name:信息名称 object:nil];
发送信息:[[NSNotificationCenter defaultCenter] postNotificationName:信息名称 object:发信息对象 userInfo:发送消息时 传递的信息(是个字典)];
代码示例:
// 注册一条通知 就是接收方接受参数的
// [NSNotificationCenter defaultCenter] 通知中心 是个单例类
// name:@"NOTIFICATIONONE"通知的名字 最好是全大写 做到见名知意
//selector:收到通知后触发的方法
//object:发送方对象, 可以写nil,如果写的话发送信息和注册时要一致
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationInfo:)
name:@"NOTIFICATIONONE" object:@"888"];
// 销毁通知 名字要和注册时一致
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"NOTIFICATIONONE" object:nil];
// 发送通知
//postNotificationName:@"NOTIFICATIONONE" 通知的名字 必须和注册时一样 否则接收不到
//userInfo:(NSDictionary *)携带的参数 是个字典
//object:@"888"必须和注册通知时一致 或者注册时不填
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONONE" object:@"888"
userInfo:@{@"UI":@"Over"}];
下面通过一个例子了解通知的用法:(通过通知来完成简单的换肤效果)
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"按钮" forState:UIControlStateNormal];
[button addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(100, 250, 100, 100);
button1.backgroundColor = [UIColor blueColor];
[button1 setTitle:@"按钮" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(actionButton1:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
// 发送通知
- (void)actionButton:(UIButton *)button
{
//postNotificationName:@"NOTIFICATIONONE" 通知的名字 必须和注册时一样 否则接收不到
//userInfo:(NSDictionary *)携带的参数 是个字典
//object:@"888"必须和注册通知时一致 或者注册时不填
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONONE" object:@"888"
userInfo:@{@"UI":@"Over"}];
}
// 发送通知
- (void)actionButton1:(UIButton *)button
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGEBARCOLORGREEN" object:nil
userInfo:nil];
}
@end
<span style="font-size:14px;">@implementation AppDelegate
-(void)dealloc
{
// 销毁通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"NOTIFICATIONONE" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"CHANGEBARCOLORGREEN" object:nil];
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// 注册通知1 改红色
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationInfo:) name:@"NOTIFICATIONONE" object:@"888"];
// 注册通知2 改绿色
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationInfo:) name:@"CHANGEBARCOLORGREEN" object:nil];
RootTabBarController *rootVC = [[RootTabBarController alloc] init];
self.window.rootViewController = rootVC;
[rootVC release];
return YES;
}
//实现接收通知的方法
- (void)notificationInfo:(NSNotification *)notification
{
// 通过通知的名字来判断发送的对应的通知 也可以注册时用不同的方法
if ([notification.name isEqualToString:@"NOTIFICATIONONE"]) {
// 1、把rootVC取出来(window的根视图控制器)
UIViewController *vc = self.window.rootViewController;
// 2、把已经设置的window的rootVC 重置为空
self.window.rootViewController = nil;
// 3、设置皮肤
// 取出NavBar 和 TabBar
// 通过调用appearance 取出要更改的 bar
UINavigationBar *navBar = [UINavigationBar appearance];
[navBar setBarTintColor:[UIColor redColor]];
UITabBar *tabBar = [UITabBar appearance];
[tabBar setBarTintColor:[UIColor redColor]];
// 4、重新赋值 window的根视图控制器
self.window.rootViewController = vc;
} else if ([notification.name isEqualToString:@"CHANGEBARCOLORGREEN"]) {
UIViewController *vc = self.window.rootViewController;
self.window.rootViewController = nil;
UINavigationBar *navBar = [UINavigationBar appearance];
[navBar setBarTintColor:[UIColor greenColor]];
UITabBar *tabBar = [UITabBar appearance];
[tabBar setBarTintColor:[UIColor greenColor]];
self.window.rootViewController = vc;
}
}
@end
</span>