iOS设计模式--总结
来源:互联网 发布:mac西柚色口红专柜价格 编辑:程序博客网 时间:2024/05/21 06:48
iOS设计模式–总结
iOS Design Patterns中介绍常用设计模式如下:
以个人经历而言,主要接触到单例模式,MVC模式,MVVM模式,观察者模式 等等。
MVC模式
MVC模式包含Model,View,和Controller,是iOS最基本的设计模式,无需过多介绍。
单例模式
常见单例模式应用:
- [NSUserDefaults standardUserDefaults]
- [UIApplication sharedApplication]
- [UIScreen mainScreen]
- [NSFileManager defaultManager]
例如判断引导页实现时需要利用[NSUserDefaults standardUserDefaults]存储当前版本信息并进行判断决定是否显示引导页。
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
通过[UIApplication sharedApplication]获取AppDelegate协议;
观察者模式
Notifications
Key-Value Observing (KVO)
Notifications
[[NSNotificationCenter defaultCenter] postNotificationName:textFieldDidBeginEditing object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidBeginEditing) name:textFieldDidBeginEditing object:nil];
-(void) textFieldDidBeginEditing:(UITextField *)textField {
[UIView animateWithDuration:0.2 delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:0.5 options:UIViewAnimationOptionTransitionCurlDown animations: ^{
self.userView.transform = CGAffineTransformMakeTranslation(0, -50);
} completion:nil];
}
通过监听键盘输入状态判断是否执行动画操作;
Key-Value Observing (KVO)
[coverImage addObserver:self forKeyPath:@”image” options:0 context:nil];
-(void)dealloc
{
[coverImage removeObserver:self forKeyPath:@”image”];
}
-(void)observeValueForKeyPath:(NSString )keyPath ofObject:(id)object change:(NSDictionary )change context:(void *)context
{
if ([keyPath isEqualToString:@”image”])
{
[indicator stopAnimating];
}
}
通过监听image属性判断是都执行动画;
备忘录模式
[NSUserDefaults standardUserDefaults defaults setBool:YES forKey:@”isLogin”];
通过NSUserDefaults记录应用登录状态;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@”isLogin”]) {
return;
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@”More” bundle:nil];
SignViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@”Sign”];
[self presentViewController:controller animated:YES completion:nil];
通过读取NSUserDefaults读取登录状态;
装饰器模式
Category
Delegate
分类和协议是Objective-c基本概念,此处略过;
命令模式
The Command design pattern encapsulates a request or action as an object. The encapsulated request is much more flexible than a raw request and can be passed between objects, stored for later, modified dynamically, or placed into a queue. Apple has implemented this pattern using the Target-Action mechanism and Invocation.
目前主要接触到 Target-Action 。
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(takePhoto)];
为button添加方法:takePhoto,当点击此button时,执行takePhoto方法。
- IOS设计模式总结
- iOS设计模式--总结
- 《iOS总结》设计模式-MickyChiang
- iOS中常用开发设计模式总结
- iOS 中的常用设计模式总结
- iOS常用设计模式总结(一)
- iOS面试总结 - 4.ios开发中的基本设计模式
- 设计模式 ----- 设计模式总结
- ios中的设计模式
- IOS 设计模式
- iOS设计模式:观察者
- iOS设计模式:观察者
- IOS 设计模式
- iOS设计模式
- ios设计模式 Category
- ios设计模式 delegate
- ios设计模式 Notification
- iOS设计模式
- 短信读写
- Python简介
- 九度oj--1047素数判定
- type=image 来提交表单
- JSswitch
- iOS设计模式--总结
- TCP/IP三次握手
- 2016-8-4夏令营入营测试总结
- Android开发之--沉浸式状态栏
- UINavigationController基础篇
- kuangbin——线段树专题 C - A Simple Problem with Integers
- 基础函数
- hdu 2089 不要62(数位DP/暴力)
- JStry&catch