销毁通知
#warning 在dealloc里面销毁通知 // name:@"NOTIFICATIONONE" 要和注册时的名字一样 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NOTIFICATIONONE" object:nil];
1. 注册通知 (在AppDelegate.m注册, 就要在
AppDelegate.m接收通知
),别忘在dealloc中销毁通知,
一句话, 在那哪个类注册, 就在哪个类接收通知
//------------------------------------- // 从注册中心注册一条通知 (就是接收方, 即接收通知的(接收参数的)) // [NSNotificationCenter defaultCenter] 通知中心 是个单例类 // name: 是通知的名字, 最好是全大写 // object: 一般填nil (用的话得一致) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationInfo:) name:@"NOTIFICATIONONE" object:@"888"]; //------------------------------------------
2. 在APPDelegate中接收通知 (以下方法实现的是注册通知里面的方法)
- (void)notificationInfo:(NSNotification *)notification
{
/********** 更改皮肤思路 ************************/
// 1. 把window的根视图控制器取出来
// 2. 把已经设置window的rootVC重置为空
// 3. 设置皮肤
// 4. 重新赋值window的根视图控制器
// 通过名字判断是哪条通知
if ([notification.name isEqualToString:@"NOTIFICATIONONE"]) {
// 1. 把window的根视图控制器取出来
UIViewController *VC = self.window.rootViewController;
// 2. 把已经设置window的rootVC重置为空
self.window.rootViewController = nil;
// 3. 设置皮肤
// (1) 取出NavBar 和 TabBar
UINavigationBar *navBar = [UINavigationBar appearance]; // 通过调用appearance方法取出要更改的bar
// 更改颜色
[navBar setBarTintColor:[UIColor redColor]];
// 取出
UITabBar *tabBar = [UITabBar appearance];
[tabBar setBarTintColor:[UIColor redColor]];// 更改颜色
// 4. 重新赋值window的根视图控制器
self.window.rootViewController = VC;
} else if ([notification.name isEqualToString:@"CHANGEBARCOLORGREEN"]) {
// 取出window的根视图控制器
UIViewController *VC = self.window.rootViewController;
// 把已经设置好的跟控制器置空
self.window.rootViewController = nil;
// 设置皮肤
// (1)取出navBar 和 TabBar
UINavigationBar *navBar = [UINavigationBar appearance];
UITabBar *tabBar = [UITabBar appearance];
// 设置bar的颜色
[navBar setBarTintColor:[UIColor greenColor]];
[tabBar setBarTintColor:[UIColor greenColor]];
// 4. 重新赋值window的跟控制器
self.window.rootViewController = VC;
}
/********** 更改皮肤思路 结束 ************************/
/*------------------------------------------------------------------*/
// 接收通知里面携带的参数
//NSDictionary *dic = notification.userInfo;
//NSLog(@"%@", dic);
NSLog(@"我收到通知了 %@, 这条通知的名字:%@, object = %@", notification.userInfo, notification.name, notification.object);
}
3. 在其他类发送通知(注意name标识要和注册的时候一致,) 发送的通知是一对多即发送一条通知,在哪个类都可以接收到
<span style="font-size:14px;">// 发送一条通知
// postNotificationName:(NSString *) 是通知的名字, 必须和注册时的名字一样, 否者那边接收不到
// object:(id) id类型的 一般填nil, (可以把self传过去)
// userInfo:(NSDictionary *) 携带的参数是个字典
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONONE" object:@"888"userInfo:@{@"UI":@"Over!"}];</span>
4. 通知是一对多的, 这是在OneVC类接收, (注册方就是接收方)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 接收通知 注册 (注意注册方, 就是接收方)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationInfo:) name:@"NOTIFICATIONONE" object:nil];
}
- (void)notificationInfo:(NSNotification *)notification
{
NSLog(@"我是oneVC , 我也收到通知了");
}
更换皮肤在接收通知的方法里写