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];
// 单例类 对象 通知中心 [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. 把已经设置window的rootVC重置为空
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);
- (void)actionNotifitation:(NSNotification*)notification
{
if ([notification.nameisEqualToString:@"NOTIFICATIONONE"]) {
// 1. 把 windows 的rootVC取出来
UIViewController *vc = self.window.rootViewController;
// 2. 把已经设置window的rootVC重置为空
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"}];
{
[[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];
{
// 页面被销毁的时候,就把通知销毁
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"NOTIFICATIONONE"object:nil];
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"NOTIFICATIONTWO"object:nil];
[_windowrelease];
[superdealloc];
}
0 0
- UI一揽子计划 24 (MVC、通知、)
- UI基础-MVC、通知
- UI一揽子计划 14 (拖控件 、)
- 随意细解:UI -- MVC、通知
- UI一揽子计划 1 (UIView UILabel)
- UI一揽子计划 2 (UITextField UIButton)
- UI一揽子计划 9 (UITableView 、UITableView 、重用机制)
- UI一揽子计划 4 (Touch 、摇一摇、响应者链)
- UI一揽子计划 6 (UIControl、UISegmentedControl、UIImageView插入数组图片、UISlider)
- UI一揽子计划 7 ( UIScrollView、 UIPageControl、综合所学简单相册)
- UI一揽子计划 8 (UINavigationController 、界面通信 、NSTimer 、NSUserDefaults)
- UI一揽子计划 11 (自定义UITableViewCell、Cell 的自适应高度)
- UI一揽子计划 12 (模态ViewController、单例、通讯录实战、)
- UI一揽子计划 13 (UITabBarController 、UITabBar、UITabBarItem 、Block传值)
- UI一揽子计划 22 (多线程概述、NSThread、NSOperationQueue 、GCD、多线程管理)
- UI一揽子计划 3 (自定义视图、UIViewControllor、屏幕旋转判断和重布局)
- UI一揽子计划 5 (Target: Action: 、Protocol Delegate、 UIImageView 、手势识别器)
- UI一揽子计划 10 (UITableView 中cell 的编辑, 增加, 删除、UITableViewController 、)
- border:none;与border:0;的区别
- UI一揽子计划 23 (动画的使用场景、UIView动画、CGAffineTransform2D仿射变换、CALayer、CAAnimation、)
- 今日学习总结循环中的一些问题
- MAT使用教程
- css3有用效果
- UI一揽子计划 24 (MVC、通知、)
- 基于模拟落体碰撞的优化算法——本科毕业论文
- Quartz2D使用
- Android手势操作(双击,长按,滑动,滚动,多点触控)
- 淘宝商品评论爬取
- 颠倒整数
- 如何定义IE的文档兼容模式
- android 自带downloadManager下载apk文件,并打开安装包
- JSON解析