iOS讲解迷惑深入浅出之通知
来源:互联网 发布:重生之网络女主播 编辑:程序博客网 时间:2024/06/08 14:41
销毁通知
#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 , 我也收到通知了");}
更换皮肤在接收通知的方法里写
0 0
- iOS讲解迷惑深入浅出之通知
- ios讲解迷惑深入浅出之GCD
- iOS讲解迷惑深入浅出之懒加载
- iOS讲解迷惑深入浅出之SAX解析和JSON 解析
- iOS讲解迷惑深入浅出之基础动画-关键帧动画
- iOS讲解迷惑深入浅出之单例类的两种写法
- iOS讲解迷惑深入浅出之复杂对象的归档
- iOS讲解迷惑深入浅出之block那点事
- iOS讲解迷惑深入浅出之开启子线程请求网络
- iOS讲解迷惑深入浅出之GCD利用屏障模拟玩家进入游戏读取数据
- iOS讲解迷惑深入浅出值UICollectionView(集合视图)
- iOS讲解迷惑之typedef枚举
- iOS讲解迷惑之集成百度地图
- iOS讲解迷惑之Core Data
- iOS讲解迷惑--LeanCloud
- iOS讲解迷惑--生成uuid
- iOS讲解迷惑--MD5加密
- iOS经典讲解之NSNotification(通知)
- uac 关闭
- 有环单向链表
- WinCE 7 CeRapiInvoke
- @property的几个参数说明
- Eclipse配置前台开发环境
- iOS讲解迷惑深入浅出之通知
- C++输入流位置的小变动对程序的影响
- 设置终端自动补全功能
- nyoj -75日期计算
- MySql基本使用方法
- 深复制和浅复制
- 广播和多播
- 【CS学习】——UML结构
- 阿里巴巴中间件性能挑战赛(MOM篇)