iOS——MVC设计模式

本文详细介绍了MVC(Model-View-Controller)设计模式,阐述了模型、视图和控制器在iOS应用开发中的角色和职责。通过实例展示了如何在登陆注册功能中应用MVC模式,强调了组件之间的通信方式和降低耦合的重要性。同时,代码示例演示了如何创建LandingModel、LandingView和LandingController,以及它们之间的交互逻辑。
摘要由CSDN通过智能技术生成

MVC是什么

MVC是Model、VIew、Controller,即模型、视图、控制器,用数据(模型)、界面显示(视图)、业务逻辑(控制器)分离的方法组织代码,是一种软件设计的规范。
请添加图片描述

模型Model

模型负责各个功能的实现(如登录、增加、删除功能)。

视图View

视图负责页面的显示和与用户的交互。在iOS应用程序开发中,所有的控件、窗口等都继承自 UIView,对应MVC中的V。UIView及其子类主要负责UI的实现,而UIView所产生的事件都可以采用委托的方式,交给UIViewController实现。

控制器Controller

控制器负责将视图与模型一一对应起来,即视图和模型是不直接联系的,控制器充当其调节器,成为一个应用程序的CPU。

MVC

MVC模式能够完成各司其职的任务模式,由于降低了各个环节的耦合性,大大优化Controller的代码量,当程序调试时,如果某一个功能没有按照既定的模式工作,可以很方便的定位到到底是Controller还是View还是Model出了问题。

ps:

1.Model和View只能通过Controller传递 , 永远不能相互通信!
2.Controller可以直接与Model对话 ( 读写调用Model ) , Model通过Notification和KVO机制与Controller间接通信。
3.Controller可以直接与View对话 , 通过outlet , 直接操作View , outlet直接对应到View中的控件 , View通过action向Controller报告事件的发生(如用户进行了Touch操作)。Controller是View的直接数据源 ( 数据一般是Controller从Model中取得并经过加工的 ) Controller是View的代理 ( delegate ) , 以同步View与Controller。

登陆注册demo

1、新建登陆和注册文件夹,在各自文件夹中新建Model、View、 Controller三个文件夹
Model中新建一个名为LandingModel的类,继承自NSObject(以登陆界面为例)
View中新建一个名为LandingView的类,继承自UIView
Controller中新建一个名为LandingController的类,继承自UIViewController
请添加图片描述

Model

//LandingModel.h

@interface LandingModel : NSObject

@property (nonatomic, strong) NSMutableArray *userArray;
@property (nonatomic, strong) NSMutableArray *passwordArray;

- (void)landingMassageInit;

@end
//LandingModel.m

#import "LandingModel.h"

@implementation LandingModel

- (void)landingMassageInit {
    _userArray = [[NSMutableArray alloc] init];
    _passwordArray = [[NSMutableArray alloc] init];
    [_userArray addObject:@"123"];
    [_passwordArray addObject:@"123"];
}

@end

View

//LandingView.h

@interface LandingView : UIView

@property (nonatomic,strong) UITextField *userTextField;
@property (nonatomic,strong) UITextField *passwordTextField;
@property (nonatomic,strong) UIButton *landingButton;
@property (nonatomic,strong) UIButton *registerButton;

@end
//LandingView.m

#import "LandingView.h"

@implementation LandingView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];
    _userTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 175, 60)];
    _userTextField.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.5];
    _userTextField.placeholder = @" 输入用户名";
    [self addSubview:_userTextField];
    
    _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 175, 60)];
    _passwordTextField.placeholder = @" 输入密码";
    _passwordTextField.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.5];
    [self addSubview:_passwordTextField];
    
    _landingButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_landingButton setTitle:@"登陆" forState:UIControlStateNormal];
    [_landingButton setFrame:CGRectMake(100, 400, 75, 30)];
    [self addSubview:_landingButton];
    
    _registerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [_registerButton setFrame:CGRectMake(200, 400, 75, 30)];
    [self addSubview:_registerButton];
    
    return self;
}
@end

Controller

//LandingController.h

#import "LandingModel.h"
#import "LandingView.h"
#import "RegisterController.h"

@interface LandingController : UIViewController

@property(nonatomic,strong) LandingView *landingView;
@property(nonatomic,strong) LandingModel *landingModel;

- (void)passName:(NSString *)name passPassword:(NSString *)password;

@end

//LandingController.m

#import "LandingController.h"

@interface LandingController ()

@end

@implementation LandingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:)name:@"tongzhi" object:nil];

    _landingView = [[LandingView alloc] init];
    _landingView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [_landingView.registerButton addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
    [_landingView.landingButton addTarget:self action:@selector(pressLanding) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_landingView];
    
    _landingModel = [[LandingModel alloc] init];
    [_landingModel landingMassageInit];
}

- (void)pressRegister {
    RegisterController* registerViewController = [[RegisterController alloc]init];
    registerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    registerViewController.existUserArray = _landingModel.userArray;
    registerViewController.registerDelegate = self;
    [self presentViewController:registerViewController animated:YES completion:nil];
}


- (void)pressLanding {
    BOOL flag = NO;
    for (int i = 0; i < _landingModel.passwordArray.count; i++) {
        if ([_landingView.userTextField.text isEqualToString:_landingModel.userArray[i]] && [_landingView.passwordTextField.text isEqualToString:_landingModel.passwordArray[i]]) {
            flag = YES;
            break;
        }
    }
    if (flag == YES) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:sure];
        [self presentViewController:alert animated:YES completion:nil];
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"帐号或密码错误!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
        [alert addAction:sure];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

- (void)passName:(NSString *)name passPassword:(NSString *)password {
    [_landingModel.userArray addObject:name];
    [_landingModel.passwordArray addObject:password];
}

@end

注册界面的View和Model和登陆界面同理,Controller中增加了协议传值将新注册的账号密码传入到登陆界面,还有对登陆中用户名的查重

运行效果

登陆失败

请添加图片描述

注册

请添加图片描述

登陆成功

请添加图片描述

重复注册

请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值