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

来源:互联网 发布:qq飞升锻体进阶数据 编辑:程序博客网 时间:2024/05/16 00:47
一.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
    [[
NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(actionNotifitation:)name:@"NOTIFICATIONONE"object:nil];
   
    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(actionNotifitation:)name:@"NOTIFICATIONTWO"object:nil];



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

    [
_windowrelease];
    [
superdealloc];
}







0 0
原创粉丝点击