UI一揽子计划 24 (MVC、通知、)

一.MVC 
Model - View - Controller
即  模型 - 视图 - 控制器
Modle (模型) 存储 处理数据 为应用程序提供数据.
View (视图)展示用户界面视图,提供用户交互 展示模型提供的数据.
Controller (控制器) 控制视图显示 处理用户交互 从模型获取数据展示在视图上 目的是解除迷行和视图之间的耦合.

C向M提出需求,直接使用M提供的数据
M向C发起通信方式:KVO  通知  ,可以在 M 发生变化时通知 C
C 负责读取M提供的数据 负责监控M的变化并进行处理
V 和 M 禁止通信


MVC 的优势:
1). 实现低耦合,减少视图和控制器之间复杂冗余的代码.
2). 提高重用性, 多个视图可以共享一个模型,多个控制器可以共享一个视图.
3). 更易于维护,M V C 独立,可以分别处理不同的变化


二. 通知
通知模式: 一个对象能够给其他任意数量的对象广播信息, 对象之间可以没有耦合关系.
NSNotification (通知),封装了要广播的信息.
NSNotificationCenter (通知中心), 管理注册接收消息对象,广播消息.
observer (观察者), 需要监测广播信息的对象,即接收信息的对象.


接收信息对象在通知中心进行注册, 包括: 信息名称, 接收信息时的处理方法.
对象通过通知中心广播信息,包括: 信息名称 信息内容等.
已经注册过的对象如果不需要接收信息时, 在通知中心注销.

一共分三个步骤:
 1). 注册

// 注册一条通知 (接受方,接受参数的)
   
// 单例类 对象 通知中心  [NSNotificationCenter defaultCenter]
   
// 通知的名字全是大写的
   
   
// 注册的时候 object 就填 nil
    [[
NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionNotifitation:) name:@"NOTIFICATIONONE" object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionNotifitation:) name:@"NOTIFICATIONTWO" object:nil];



接收信息: 
// 实现接受通知的方法
- (
void)actionNotifitation:(NSNotification *)notification
{
   
if ([notification.name isEqualToString:@"NOTIFICATIONONE"]) {
       
// 1. windows rootVC 取出来
       
       
UIViewController *vc = self.window.rootViewController;
       
       
       
// 2. 把已经设置window rootVC 重置为空
       
       
       
self.window.rootViewController = nil;
       
// 3. 设置皮肤
       
// 取出NAVBar tabBar
       
// 通过调用 appearance 取出 要更改的Bar
       
UINavigationBar *navBar = [UINavigationBar appearance];
       
UITabBar *tabBar = [UITabBar appearance];
       
       
// 更改颜色
        [navBar
setBarTintColor:[UIColor redColor]];
        [tabBar
setBarTintColor:[UIColor redColor]];
       
// 4. 重新赋值 window 的根视图控制器
       
self.window.rootViewController = vc;
    }
else if ([notification.name isEqualToString:@"NOTIFICATIONTWO"]){
       
UIViewController *vc = self.window.rootViewController;
       
self.window.rootViewController = nil;
       
UINavigationBar *navBar = [UINavigationBar appearance];
       
UITabBar *tabBar = [UITabBar appearance];
        [navBar
setBarTintColor:[UIColor grayColor]];
        [tabBar
setBarTintColor:[UIColor grayColor]];
       
self.window.rootViewController = vc;
    }
   
   
// 接受发过来的信息(携带的参数)
   
NSDictionary *dic = notification.userInfo;
   
NSLog(@"我收到通知了%@, 通知的名字:%@, object:%@", dic, notification.name, notification.object);
}
 2). 发送信息
- (void)actionButton1:(UIButton *)button
{
    [[
NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONTWO" object:nil userInfo:nil];
}
// 发送通知
- (
void)actionButton:(UIButton *)button
{
   
   
// 发送一条通知  名字必须一样 否则接受不到
   
   
// userInfo 才是携带的参数 是个字典
   
   
// 构建一个字典
   
   
// object 一般 nil
   
   
// 携带参数就放字典里面
    [[
NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONONE" object:self userInfo:@{@"UI":@"Over"}];
}
 3). 注销
- (void)dealloc
{
   
// 页面被销毁的时候,就把通知销毁
    [[
NSNotificationCenter defaultCenter] removeObserver:self name:@"NOTIFICATIONONE" object:nil];
    [[
NSNotificationCenter defaultCenter] removeObserver:self name:@"NOTIFICATIONTWO" object:nil];

    [
_window release];
    [
super dealloc];
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值