做公司网站的费用,gui设计师,网站建设方案报价表,育婴师培训目录
1. 引言
2.概念
1.Model
1.职责
2.实现
3.和Controller通信
1.Contrller直接访问Model
2.通过委托(Delegate)模式
3.通知 4.KVO
4.设计的建议
2.View
1.职责
2.实现
3.和Controller通信
1. 目标-动作#xff08;Target-Action#xff09;模式
2…目录
1. 引言
2.概念
1.Model
1.职责
2.实现
3.和Controller通信
1.Contrller直接访问Model
2.通过委托(Delegate)模式
3.通知 4.KVO
4.设计的建议
2.View
1.职责
2.实现
3.和Controller通信
1. 目标-动作Target-Action模式
2. 委托Delegate模式
3. 通知Notification模式
4. KVC 和 KVOKey-Value Coding 和 Key-Value Observing
5. Block 回调
3.Controller
1.职责
2.实现
3.通信
3. MVC设计模式的优缺点
1.优点
2. 缺点
4. 实践中的建议 1. 引言 MVCModel-View-Controller是一种常见的软件设计模式旨在将应用程序的逻辑、用户界面和输入进行分离。 在iOS开发中MVC帮助开发者组织代码提高可维护性和可扩展性使得项目结构更加清晰。 本文将介绍MVC的基本概念、在iOS中的具体实现、优缺点、实践建议以及其他常见的架构模式。
2.概念 MVC的全称和基本概念MVC即Model-View-Controller分别代表模型、视图和控制器三个部分。 各个组件的职责
Model负责数据和业务逻辑包括数据的获取、保存、处理等。View负责用户界面的展示直接与用户进行交互。Controller:负责协调Model和View处理用户输入,并将其转化为对Model的操作或View的更新。 在我们面试的过程中MVC基本上是面试必问的基础性的问题。笔者总结了两点MVC就是Controller拿Model的数据区更新View,同时Model和View之间是不互相通信的它们之间的联系是通过Controller来桥接。 下图是斯坦福大学网上iOS公开课中的关于MVC的讲解感觉总结的非常不错。 图1.MVC设计模式图 下面逐个分析下MVC中的每一个模块。
1.Model Model是数据模型的意思在iOS的设计模式中主要负责处理数据和业务逻辑。
1.职责 Model的主要职责如下 1.数据存储Model负责存储应用程序的数据。数据可以来自本地存储、网络请求或其他外部数据源。 2.数据操作Model包含操作数据的方法例如创建、读取、更新和删除CRUD操作。 3.业务逻辑Model还负责处理应用程序的业务逻辑例如数据验证、计算和转换等。 4.数据通知当数据发生变化时Model可以通过通知或回调机制通知Controller以便更新视图。
2.实现 在实际的开发过程中Model通常是一个或者多个类这些类可以代表数据库中的表、网络请求的响应或其他数据结构 下面是一个简单的Model类示例演示了如何定义和操作Model
// User.h
#import Foundation/Foundation.hinterface User : NSObject
property (nonatomic, strong) NSString *name;
property (nonatomic, assign) NSInteger age;- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;
- (void)updateName:(NSString *)newName;
end// User.m
#import User.himplementation User- (instancetype)initWithName:(NSString *)name age:(NSInteger)age {self [super init];if (self) {_name name;_age age;}return self;
}- (void)updateName:(NSString *)newName {_name newName;// 通知Controller数据已更新[[NSNotificationCenter defaultCenter] postNotificationName:UserUpdatedNotification object:self];
}end 在这个示例中User类是一个简单的Model类包含用户的姓名和年龄。它还提供了一个方法来更新用户的姓名并在更新后通过通知机制通知Controller。
3.和Controller通信 Model通常不会直接与View通信而是通过Controller进行中介。Controller负责监听Model的数据变化并相应地更新View。 Model和Controller中的通信方式有以下几种方式
1.Contrller直接访问Model Controller可以直接访问model的属性和方法。 Controller可以直接访问和操作Model的属性和方法以获取或更新数据。这是最直接和常见的方式。 我们以下面的更新App版本号为例 图1.更新app版本号 因为Controller直接持有Model,因此我们直接可以直接修改model数据 #pragma mark -- AboutViewDelegate - (void)didTapUpdateButton{ self.aboutModel.appVersion [NSString stringWithFormat:%ld.%ld.%ld,[self generateRandomNumber:10],[self generateRandomNumber:99],[self generateRandomNumber:99]]; [self.aboutView configureWithModel:self.aboutModel]; } - (NSInteger)generateRandomNumber:(NSInteger)random { return arc4random_uniform(10) 1; // 生成1到10之间的随机数 } 完整代码如下
#import AboutViewController.h
#import AboutView.h
#import AboutModel.h
#import Masonry.hinterface AboutViewController ()AboutViewDelegateproperty (nonatomic, strong) AboutView *aboutView;
property (nonatomic, strong) AboutModel *aboutModel;endimplementation AboutViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor [UIColor whiteColor];self.title 关于;[self setupModel];[self setupView];
}- (void)setupModel {// 假设我们从某处获取了应用的名称和版本号NSString *appName myApp;NSString *appVersion 1.0.0;self.aboutModel [[AboutModel alloc] initWithAppName:appName appVersion:appVersion];
}- (void)setupView {self.aboutView [[AboutView alloc] initWithFrame:self.view.bounds];self.aboutView.delegate self;[self.view addSubview:self.aboutView];[self.aboutView configureWithModel:self.aboutModel];
}#pragma mark -- AboutViewDelegate
- (void)didTapUpdateButton{self.aboutModel.appVersion [NSString stringWithFormat:%ld.%ld.%ld,[self generateRandomNumber:10],[self generateRandomNumber:99],[self generateRandomNumber:99]];[self.aboutView configureWithModel:self.aboutModel];}
- (NSInteger)generateRandomNumber:(NSInteger)random {return arc4random_uniform(10) 1; // 生成1到10之间的随机数
}
end
2.通过委托(Delegate)模式 Model可以定义一个协议protocolController遵循这个协议并实现其方法。Model通过委托将数据变化通知给ControllerController则通过协议的方法来响应这些变化。 还以上面的demo为例我们还可以把更新的方法传递到Controller,让controller去处理。
#import AboutViewController.h
#import AboutView.h
#import AboutModel.h
#import Masonry.hinterface AboutViewController ()AboutViewDelegate,AboutModelDelegateproperty (nonatomic, strong) AboutView *aboutView;
property (nonatomic, strong) AboutModel *aboutModel;endimplementation AboutViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor [UIColor whiteColor];self.title 关于;[self setupModel];[self setupView];
}- (void)setupModel {// 假设我们从某处获取了应用的名称和版本号NSString *appName myApp;NSString *appVersion 1.0.0;self.aboutModel [[AboutModel alloc] initWithAppName:appName appVersion:appVersion];self.aboutModel.delegate self;
}- (void)setupView {self.aboutView [[AboutView alloc] initWithFrame:self.view.bounds];self.aboutView.delegate self;[self.view addSubview:self.aboutView];[self.aboutView configureWithModel:self.aboutModel];
}#pragma mark -- AboutViewDelegate
- (void)didTapUpdateButton{NSLog(didTapUpdateButton);[self.aboutModel updateAppVersion:[NSString stringWithFormat:%ld.%ld.%ld,[self generateRandomNumber:10],[self generateRandomNumber:99],[self generateRandomNumber:99]]];}
#pragma mark -- AboutModelDelegate
- (void)didUpdateAppVersion:(NSString *)appVersion{NSLog(AboutModelDelegate);[self.aboutView configureWithModel:self.aboutModel];
}- (NSInteger)generateRandomNumber:(NSInteger)random {return arc4random_uniform(10) 1; // 生成1到10之间的随机数
}
end3.通知 iOS的通知机制NSNotificationCenter允许在应用程序的不同部分之间发送消息。Controller可以监听特定的通知以便在Model发出通知时执行相应的操作。
// MyModel.h
#import Foundation/Foundation.hinterface MyModel : NSObject
property (nonatomic, strong) NSString *data;- (void)updateData:(NSString *)newData;
end// MyModel.m
#import MyModel.himplementation MyModel- (void)updateData:(NSString *)newData {_data newData;[[NSNotificationCenter defaultCenter] postNotificationName:DataUpdatedNotification object:newData];
}end// MyViewController.m
#import MyViewController.h
#import MyModel.himplementation MyViewController- (void)viewDidLoad {[super viewDidLoad];self.myModel [[MyModel alloc] init];[[NSNotificationCenter defaultCenter] addObserver:self selector:selector(handleDataUpdated:) name:DataUpdatedNotification object:nil];[self.myModel updateData:New Data];
}- (void)handleDataUpdated:(NSNotification *)notification {NSString *updatedData (NSString *)notification.object;NSLog(Model data updated: %, updatedData);
}- (void)dealloc {[[NSNotificationCenter defaultCenter] removeObserver:self];
}end 4.KVO KVO是一种观察模式允许对象监听其他对象属性的变化。Controller可以监听Model的属性变化以便在属性发生变化时进行相应的处理。
// MyModel.h
#import Foundation/Foundation.hinterface MyModel : NSObject
property (nonatomic, strong) NSString *data;
end// MyModel.m
#import MyModel.himplementation MyModel- (instancetype)init {self [super init];if (self) {_data Hello, MVC!;}return self;
}end// MyViewController.m
#import MyViewController.h
#import MyModel.himplementation MyViewController- (void)viewDidLoad {[super viewDidLoad];self.myModel [[MyModel alloc] init];[self.myModel addObserver:self forKeyPath:data options:NSKeyValueObservingOptionNew context:nil];self.myModel.data Updated Data;
}// 处理属性变化
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionaryNSKeyValueChangeKey,id *)change context:(void *)context {if ([keyPath isEqualToString:data]) {NSLog(Model data changed: %, change[NSKeyValueChangeNewKey]);}
}- (void)dealloc {[self.myModel removeObserver:self forKeyPath:data];
}end
4.设计的建议
保持Model的独立性Model不应该直接依赖View或Controller。这样可以使Model更加独立和可复用。使用协议和委托可以使用协议和委托来定义Model与Controller的交互接口增强代码的灵活性和可维护性。使用数据持久化框架在处理复杂数据模型时可以使用Core Data、Realm等数据持久化框架来简化数据管理和存储。注意线程安全在多线程环境下操作Model时需要注意线程安全避免数据竞争和不一致问题。
2.View
1.职责 view是视图的意思。在iOS MVCModel-View-Controller设计模式中View 负责管理应用程序的用户界面UI部分。它是用户与应用程序进行交互的界面层负责显示数据和响应用户操作。以下是对 View 的详细介绍
展示数据View 负责根据 Model 提供的数据来展示用户界面。它不直接处理数据的逻辑只是按照 Controller 提供的数据进行展示。View 的任务是将数据直观地呈现给用户。相应用户输入View 负责处理用户的输入如点击按钮、滑动屏幕等操作并将这些事件传递给 Controller 进行处理。View 本身不包含业务逻辑而是将事件传递给负责逻辑处理的 Controller。独立于业务逻辑View 不应包含应用程序的业务逻辑或数据处理逻辑。它与 Model 之间没有直接的交互所有的数据交互都通过 Controller 来完成。这种独立性使得 View 可以很容易地进行修改或替换而不会影响到应用程序的其他部分。
2.实现 在 iOS 应用程序中View 的典型组件包括UIView、UILabel、UIButton、UIImageView、UITableView等。
3.和Controller通信
1. 目标-动作Target-Action模式 目标-动作模式是 iOS 中常用的一种通信方式通常用于处理用户界面控件如按钮、开关、滑块等的事件。当用户与控件交互时控件会向其指定的目标通常是 Controller发送一个动作消息。 // 在View中设置目标和动作 UIButton *button [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:Press Me forState:UIControlStateNormal]; [button addTarget:self action:selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; // Controller 中实现动作方法 - (void)buttonPressed:(UIButton *)sender { NSLog(Button was pressed); } 2. 委托Delegate模式 托模式是一种常见的设计模式允许一个对象向另一个对象通知发生的事件。在 iOS 中许多 UIKit 组件都使用委托模式例如 UITableView 和 UICollectionView。Controller 通常会作为 View 的委托对象以便处理 View 的事件。 // 自定义View中的委托协议 protocol CustomViewDelegate NSObject - (void)customViewDidSomething:(UIView *)view; end interface CustomView : UIView property (nonatomic, weak) idCustomViewDelegate delegate; end implementation CustomView - (void)doSomething { [self.delegate customViewDidSomething:self]; } end // 在Controller中实现委托方法 interface ViewController () CustomViewDelegate end implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CustomView *customView [[CustomView alloc] init]; customView.delegate self; [self.view addSubview:customView]; } - (void)customViewDidSomething:(UIView *)view { NSLog(CustomView did something); } end 3. 通知Notification模式 通知中心NSNotificationCenter是一种广播机制允许对象通过发布和观察通知进行通信。通知是一对多的通信机制适用于需要向多个对象广播信息的情况。 // 发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:CustomNotification object:nil]; // 观察通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:selector(handleNotification:) name:CustomNotification object:nil]; // 处理通知的方法 - (void)handleNotification:(NSNotification *)notification { NSLog(Received CustomNotification); } // 移除观察者 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } 4. KVC 和 KVOKey-Value Coding 和 Key-Value Observing KVC 和 KVO 是 Cocoa 提供的动态属性访问和观察机制。KVC 允许通过字符串键来访问对象的属性KVO 允许对象观察其他对象属性的变化。 // Model类 interface Person : NSObject property (nonatomic, strong) NSString *name; end implementation Person end // Controller中使用KVO观察属性变化 interface ViewController () property (nonatomic, strong) Person *person; end implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.person [[Person alloc] init]; [self.person addObserver:self forKeyPath:name options:NSKeyValueObservingOptionNew context:nil]; } // 实现观察者方法 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionaryNSKeyValueChangeKey,id *)change context:(void *)context { if ([keyPath isEqualToString:name]) { NSLog(Persons name changed to %, change[NSKeyValueChangeNewKey]); } } // 移除观察者 - (void)dealloc { [self.person removeObserver:self forKeyPath:name]; } end 5. Block 回调 Block 回调是一种轻量级的替代委托模式的方法。它允许在执行某些操作后调用一个代码块block来处理结果。Block 回调简化了委托模式不需要定义协议和委托对象。 // 自定义View类使用Block回调 interface CustomView : UIView property (nonatomic, copy) void (^buttonPressedBlock)(void); end implementation CustomView - (instancetype)init { self [super init]; if (self) { UIButton *button [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:Press Me forState:UIControlStateNormal]; [button addTarget:self action:selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } return self; } - (void)buttonPressed { if (self.buttonPressedBlock) { self.buttonPressedBlock(); } } end // 在Controller中设置Block回调 interface ViewController () end implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CustomView *customView [[CustomView alloc] init]; customView.buttonPressedBlock ^{ NSLog(Button was pressed in CustomView); }; [self.view addSubview:customView]; } end 3.Controller
1.职责 在 iOS 的 MVCModel-View-Controller设计模式中Controller 是连接 Model 和 View 的中介负责处理应用程序的业务逻辑和用户交互。Controller 的主要职责是从 Model 中获取数据更新 View并响应用户输入。以下是对 Controller 的详细介绍 1.管理视图 Controller 负责创建和配置 View并将 Model 中的数据传递给 View 进行显示。当 Model 的数据发生变化时Controller 也会通知 View 进行相应的更新。 2.处理用户输入 Controller 响应用户在 View 上的各种操作例如点击按钮、滑动手势等。它会根据用户的输入更新 Model并通过 View 来反映这些变化。 3.业务逻辑 Controller 包含应用程序的业务逻辑和数据处理逻辑。它从 Model 获取数据、处理数据并将处理结果传递给 View。 3.中介作用 Controller 充当 Model 和 View 之间的中介。它负责协调这两者之间的通信使得 View 不直接与 Model 交互从而保持两者的解耦。
2.实现 在 iOS 开发中UIViewController 是 Controller 的基类。每个 UIViewController 都管理一个 UIView并与该视图相关联的 Model 进行交互。
3.通信 Controller和Model以及View之间的通信可以看上面的章节。
3. MVC设计模式的优缺点
1.优点 1.易于测试和维护 各个组件独立便于单独测试和维护。 2.代码复用性强 View和Model可以在不同的Controller中重复使用提高代码复用性。
2. 缺点 1.可能导致Controller过于臃肿 在处理复杂逻辑时Controller可能变得过于庞大难以维护。 2.View和Controller之间的耦合度较 由于Controller直接管理View导致二者耦合度较高不利于独立开发和测试。 3.复杂项目中的可扩展性问题 在大型项目中MVC可能难以满足复杂需求需要引入其他设计模式进行补充。
4. 实践中的建议
1.如何避免Controller臃肿 使用Helper类或Extensions分担部分逻辑。 将业务逻辑移到Model层尽量保持Controller的简洁。 2.减少View和Controller的耦合 使用协议和委托模式来解耦。 利用通知和KVOKey-Value Observing机制实现低耦合的数据传递。 3.代码组织和文件结构的最佳实践 将Model、View、Controller分别放在不同的文件夹中。 使用命名空间或模块化方式组织代码。