UI一揽子计划 24 (MVC、通知、)
2015-09-24 22:00
627 查看
一.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];
}
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];
}
相关文章推荐
- UI一揽子计划 23 (动画的使用场景、UIView动画、CGAffineTransform2D仿射变换、CALayer、CAAnimation、)
- hdu1159 Common Subsequence (最长公共子串)
- UI Overview
- jquey 92 json.parse json.stringify
- IOS每天15个注意点系列之UI-tableView
- XMPP didReceivePresenceSubscriptionRequest函数不执行解决办法
- HDU - 3732 Ahui Writes Word(多重背包)
- Windows 7 and Ubuntu install issues
- lintcode-Segmemt Tree Build II-439
- 【IOS学习笔记】UITableView隐藏多余分割线
- 【IOS学习笔记】UITableView 点击隐藏键盘 且不影响其他事件
- Java序列化机制中的类版本问题 serialVersionUID的静态字段 含义
- 【UI初级--连载八】-------------表视图(UITableView)
- zoj 3820 Building Fire Stations 树的中心
- baguetteBox.js响应式画廊插件(纯JS)
- 【UI初级------连载七】标签控制器
- 【UIKit-124-11】#import <UIKit/UIView.h>
- POJ 3061 Subsequence (迟取法_预处理,连续子序列)
- Android samples API Demos之UI篇2(ActivitySceneTransitionBasic)
- unsigned int reverse_bit(unsigned int value)