iOS——MVC设计模式
什么是MVC
MVC是Model-View-Controller的簡寫,它表示的是一種常見的客戶端軟件開發框架。可見他是由三個部分組成的。
下面解釋一下這三個部分:
Model:即為模型,用來存儲業務數據, 具有很好的復用性,我們也可以在這個模型中完成對數據庫中數據的增刪查改。
View:即為視圖,用來顯示業務需要的UI控件,以實現人機交互。
Controller:控制器,這里可以將其理解為一個CPU,負責處理View和Mode的事件,應用程序中所有的工作都由控制器統一調控。比如用戶的操作就是由控制器實現的。
MVC模式能夠完成各司其職的任務模式,由于降低了各個環節的耦合性,大大優化Controller的代碼量,當程序調試時,如果某一個功能沒有按照既定的模式工作,可以很方便的定位到到底是Controller還是View還是Model出了問題,而且還利于程序的可復用性
MVC的原理
上面大概介紹了MVC的組成,下面具體介紹一下MVC三個組成之間的邏輯關系,下圖生動形象的描述了這個關系:
1、 Controller和View之間可以通信,Controllor通過outlet(輸出口)控制View,View可以通過target-action、delegate或者data source(想想UITableVeiwDatasource)來和Controller通信;
2、 Controller在接收到View傳過來的交互事件(View就是完成讓人和程序的交互的呀,比如按B1按鈕)之后,經過一些判斷和處理,把需要Model處理的事件遞交給Model處理(比如剛才的例子中的保存到數據庫),Controller對Model使用的是API;
3、 Model在處理完數據之后,如果有需要,會通過Notification或者KVO的方式告知Controller,事件已經處理完,Controller再經過判斷和處理之后,考慮下一步要怎么辦(是默默無聞的在后臺操作,還是需要更新View,這得看Controller的“臉色”行事)。這里的無線天線很有意思,Model只負責發送通知,具體誰接收這個通知并處理它,Model并不關心,這一點非常重要,是理解Notification模式的關鍵。
4、 Model和View之間不直接通信!
上面部分摘自:
實際案例講解iOS——MVC設計模式
如何使用MVC設計模式
下面舉了一個登錄注冊的小Demo。
首先我們得將項目的文件根據MVC創建,如下所示:
一般來說創建幾個視圖就得創建幾套MVC文件,
這里的LandModel繼承于NSObject,
LandView繼承于UIView。
LandController繼承于VIewController。
下面將介紹在三個文件中都應該寫什么。
Model文件
在上面已經介紹過,Model主要用來存儲業務數據,這里用來存儲登錄時的初始密碼。
// LandModel.h // MVC Demo // // Created by 差不多先生 on 2021/9/7. //#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface LandModel : NSObject @property (nonatomic, strong) NSString* idString; @property (nonatomic, strong) NSString* passwordString; - (void) landModelInit; @endNS_ASSUME_NONNULL_END // LandModel.m // MVC Demo // // Created by 差不多先生 on 2021/9/7. //#import "LandModel.h"@implementation LandModel - (void) landModelInit {_idString = [[NSString alloc] init];_passwordString = [[NSString alloc] init];_idString = @"123";_passwordString = @"123"; } @endView文件
上面介紹過,View文件是控制UI控件的文件。
// Created by 差不多先生 on 2021/9/7. //#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface LandView : UIView @property (nonatomic, strong) UITextField* idTextField; @property (nonatomic, strong) UITextField* passwordTextField; @property (nonatomic, strong) UIButton* landButton; @property (nonatomic, strong) UIButton* registerButton;@endNS_ASSUME_NONNULL_END // LandView.m // MVC Demo // // Created by 差不多先生 on 2021/9/7. //#import "LandView.h"@implementation LandView - (id)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];self.backgroundColor = [UIColor orangeColor];_idTextField = [[UITextField alloc] init];_idTextField.frame = CGRectMake(80, 150, 250, 40);_idTextField.backgroundColor = [UIColor whiteColor];[self addSubview:_idTextField];_idTextField.placeholder = @"please input your id";_passwordTextField = [[UITextField alloc] init];_passwordTextField.frame = CGRectMake(80, 250, 250, 40);_passwordTextField.placeholder = @"please input your password";_passwordTextField.backgroundColor = [UIColor whiteColor];_passwordTextField.secureTextEntry = YES;[self addSubview:_passwordTextField];_landButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];[_landButton setTitle:@"Land" forState:UIControlStateNormal];_landButton.frame = CGRectMake(120, 350, 70, 30);_registerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];[_registerButton setTitle:@"register" forState:UIControlStateNormal];_registerButton.frame = CGRectMake(210, 350, 70, 30);[self addSubview:_landButton];[self addSubview:_registerButton];return self; }注意這里我們重寫了initWithFrame方法,這是因為我們創建了一個UIView的子類,這時候將調用這個方法來實例化對象。
特別注意,如果在子類中重載initWithFrame方法,必須先調用父類的initWithFrame方法。在對自定義的UIView子類進行初始化操作。)
我們重寫完方法后,只需在Controller文件中直接創建UIView對象即可。
Controller文件
控制器主要控制各種事件操作,例如我們點擊登錄時進行的邏輯操作,是在Controller文件中書寫的,同樣一些代理方法也可以在此文件中實現。
// // LandController.m // MVC Demo // // Created by 差不多先生 on 2021/9/7. //#import "LandController.h" #import "LandView.h" #import "LandModel.h" #import "RegisterViewController.h" @interface LandController () @property (nonatomic, strong) LandView* landView; @property (nonatomic, strong) LandModel* landModel; @end@implementation LandController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view._landView = [[LandView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];[self.view addSubview:_landView];_landModel = [[LandModel alloc] init];[_landModel landModelInit];[_landView.landButton addTarget:self action:@selector(SignButtonPress) forControlEvents:UIControlEventTouchUpInside];[_landView.registerButton addTarget:self action:@selector(registerPress) forControlEvents:UIControlEventTouchUpInside]; } - (void) SignButtonPress {if ([_landView.idTextField.text isEqualToString:@""] || [_landView.passwordTextField.text isEqualToString:@""]) {UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"請完整輸入" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:nil];[alertController addAction:sureAction];[self presentViewController:alertController animated:YES completion:nil];} else if ([_landView.idTextField.text isEqual:_landModel.idString] && [_landView.passwordTextField.text isEqual:_landModel.passwordString]) {UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"登錄成功" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:nil];[alertController addAction:sureAction];[self presentViewController:alertController animated:YES completion:nil];} else {UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"賬號或密碼錯誤" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:nil];[alertController addAction:sureAction];[self presentViewController:alertController animated:YES completion:nil];} } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {[_landView.idTextField resignFirstResponder];[_landView.passwordTextField resignFirstResponder];} - (void)registerPress {RegisterViewController* viewController = [[RegisterViewController alloc] init];viewController.modalPresentationStyle = UIModalPresentationFullScreen;viewController.delegate = self;[self presentViewController:viewController animated:YES completion:nil]; } - (void)returnId:(NSString *)ida andPass:(NSString *)password {_landModel.idString = ida;_landModel.passwordString = password; } /* #pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller. } */@end總結
以上是生活随笔為你收集整理的iOS——MVC设计模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用例建模
- 下一篇: 机械中计算机的应用研究,机械设计制造及其