当前位置:   article > 正文

iOS-- MVC模式_ios mvc

ios mvc


MVC模式简介

MVC模式是iOS编程中使用最频繁和提到的最多次的设计模式之一

简介:

MVC模式–全称Model View Controller,模型(model)视图(view)控制器(controller)
View–存放视图
Model–模型,模型一般都有很好的可复用性,统一管理一些数据。
Controller–控制器,充当一个CPU的功能,即该应用程序所有的工作都由Controller统一调控。负责处理View和Model的事件。

优点:

降低了各个环节耦合性,优化Controller的代码量。

原理:

MVC模式需要综合使用target-action,delegate,Notification,KVO模式等
Controller和View之间可以通信,Controller通过outlet(输出口)控制View,View可以通过target-action,delegate或者data source来和Controller通信
controller在接收到View传过来的交互时间后,经过一些判断和处理,把需要Model处理的事件递交给Model处理,Controller对Model使用的是API
Model在处理完数据后,如果有需要,会通过Notification或者KVO的方式告知Controller事件处理完毕。Controller经过判断和处理,考虑下一步要怎么做。
Model和View之间不直接通信

图片来自斯坦福大学的iOS一堂公开课上使用的实例图
在这里插入图片描述

实例

我们使用一个登陆注册案例来看看M、V、C各个部分分别干了什么样的工作
在这里插入图片描述

  • 新建一个项目,在新建名为VView,RView的类,继承UIView
  • 新建MModel,RModel的类继承NSObject
  • 新建RViewControll的类继承UIViewController
  • 在登陆注册的View里有两个UITextField和UIButton按钮
  • 将存放账号和密码的数组放入MModel中
  • 在ViewController中使用传值方法将登陆注册的账号密码传入相应界面。
  • 在相应的界面接收传递的值

登陆页面
在这里插入图片描述
注册页面
在这里插入图片描述


//  RModel.h
//  MVC_2
//
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface RModel : NSObject

@end

NS_ASSUME_NONNULL_END



//  RModel.m
//  MVC_2
//
//

#import "RModel.h"

@implementation RModel

@end


//  RView.h
//  MVC_2
//
//
#import <UIKit/UIKit.h>


@interface RView : UIView

@property (nonatomic, strong) UIButton *loadBtn;
@property (nonatomic, strong) UIButton *registerBtn;
@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *passTextField;

- (void)InitView; //view初始化

@end



//  RView.m
//  MVC_2
//
//

#import "RView.h"

@implementation RView

- (void)InitView {
    
    _loadBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_loadBtn setFrame:CGRectMake(80, 400, 100, 50)];
    [_loadBtn setTitle:@"  back" forState:UIControlStateNormal];
    [_loadBtn setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self addSubview:_loadBtn];
    
    _registerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_registerBtn setFrame:CGRectMake(230, 400, 100, 50)];
    [_registerBtn setTitle:@"  ok" forState:UIControlStateNormal];
    [_registerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:_registerBtn];
    
    _nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 300, 50)];
    _nameTextField.layer.masksToBounds = YES;
    _nameTextField.layer.cornerRadius = 5;
    _nameTextField.layer.borderWidth = 2;
    _nameTextField.layer.borderColor = [UIColor grayColor].CGColor;
    _nameTextField.placeholder = @"  nameWord";
    [self addSubview:_nameTextField];
    
    _passTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 280, 300, 50)];
    _passTextField.layer.masksToBounds = YES;
    _passTextField.layer.cornerRadius = 5;
    _passTextField.layer.borderWidth = 2;
    _passTextField.layer.borderColor = [UIColor grayColor].CGColor;
    _passTextField.secureTextEntry = YES;
    _passTextField.placeholder = @"  passWord";
    [self addSubview:_passTextField];
    
}


@end




//  RViewController.h
//  MVC_2
//
//
#import <UIKit/UIKit.h>
#import "RView.h"
#import "RModel.h"

@protocol RegisterDelegate <NSObject>

- (void)passName:(NSString *)name passPass:(NSString *)pass;

@end

@interface RViewController : UIViewController

@property (nonatomic, strong) RView *myView;
@property (nonatomic, strong) RModel *myModel;
@property id <RegisterDelegate> registerDelegate;

@end


//  RViewController.m
//  MVC_2
//
//

#import "RViewController.h"
#import "ViewController.h"

@interface RViewController ()

@end

@implementation RViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _myView = [[RView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [_myView InitView];
    [_myView.loadBtn addTarget:self action:@selector(pressLoad) forControlEvents:UIControlEventTouchUpInside];
    [_myView.registerBtn addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_myView];
    
    _myModel = [[RModel alloc] init];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)pressLoad {
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

- (void)pressRegister{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"你已成功注册!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:^(UIAlertAction *sure) {
        if ([self->_registerDelegate respondsToSelector:@selector(passName:passPass:)]) {
            [self->_registerDelegate passName:self->_myView.nameTextField.text passPass:self->_myView.passTextField.text];
        }
         [self dismissViewControllerAnimated:NO completion:nil];
    }];
    [alert addAction:sure];
    [self presentViewController:alert animated:NO completion:nil];
    
}




@end




//  MModel.h
//  MVC_2
//
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MModel : NSObject


@property (nonatomic, strong) NSMutableArray *nameArr;
@property (nonatomic, strong) NSMutableArray *passArr;

- (void)modelInit;
@end

NS_ASSUME_NONNULL_END


//  MModel.m
//  MVC_2
//
//

#import "MModel.h"

@implementation MModel

- (void)modelInit {
    
    _nameArr = [[NSMutableArray alloc] init];
    _passArr = [[NSMutableArray alloc] init];
    [_nameArr addObject:@"123"];
    [_passArr addObject:@"456"];
    
}

@end


//  VView.h
//  MVC_2
//
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface VView : UIView

@property (nonatomic, strong) UIButton *loadBtn;
@property (nonatomic, strong) UIButton *registerBtn;
@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *passTextField;

- (void)InitView; //view初始化
@end

NS_ASSUME_NONNULL_END


//  VView.m
//  MVC_2
//

#import "VView.h"

@implementation VView
- (void)InitView {
    
    _loadBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_loadBtn setFrame:CGRectMake(80, 400, 100, 50)];
    [_loadBtn setTitle:@"  load" forState:UIControlStateNormal];
    [_loadBtn setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self addSubview:_loadBtn];
    
    _registerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_registerBtn setFrame:CGRectMake(230, 400, 100, 50)];
    [_registerBtn setTitle:@"  register" forState:UIControlStateNormal];
    [_registerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:_registerBtn];
    
    _nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 300, 50)];
    _nameTextField.layer.masksToBounds = YES;
    _nameTextField.layer.cornerRadius = 5;
    _nameTextField.layer.borderWidth = 2;
    _nameTextField.layer.borderColor = [UIColor grayColor].CGColor;
    _nameTextField.placeholder = @"  nameWord";
    [self addSubview:_nameTextField];
    
    _passTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 280, 300, 50)];
    _passTextField.layer.masksToBounds = YES;
    _passTextField.layer.cornerRadius = 5;
    _passTextField.layer.borderWidth = 2;
    _passTextField.layer.borderColor = [UIColor grayColor].CGColor;
    _passTextField.placeholder = @"  passWord";
    [self addSubview:_passTextField];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

//  ViewController.h
//  MVC_2
//
//

#import <UIKit/UIKit.h>

#import "RViewController.h"
#import "VView.h"
#import "MModel.h"
@interface ViewController : UIViewController
<RegisterDelegate>

@property (nonatomic, strong) VView *myView;
@property (nonatomic, strong) MModel *myModel;


@end


//  ViewController.m
//  MVC_2
//
//  Created by 王璐 on 2022/9/7.
//

#import "ViewController.h"
#import "newViewController.h"
#import "RViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _myView = [[VView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [_myView InitView];
    [_myView.loadBtn addTarget:self action:@selector(pressLoad) forControlEvents:UIControlEventTouchUpInside];
    [_myView.registerBtn addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_myView];
    
    _myModel = [[MModel alloc] init];
    [_myModel modelInit];
    
}

- (void)pressLoad {
    
    for (int i = 0; i < _myModel.nameArr.count; i++) {
        if ([_myView.nameTextField.text isEqualToString: _myModel.nameArr[i]] && [_myView.passTextField.text isEqualToString:_myModel.passArr[i]]) {
            newViewController *new = [[newViewController alloc] init];
            [self presentViewController:new animated:NO completion:nil];
        }
    }
}

- (void)pressRegister {
    
    RViewController *RegistViewController = [[RViewController alloc] init];
    RegistViewController.registerDelegate = self;
    [self presentViewController:RegistViewController animated:NO completion:nil];
    
    
}

- (void)passName:(NSString *)name passPass:(NSString *)pass {
    
    [_myModel.nameArr addObject:name];
    [_myModel.passArr addObject:pass];
    
}




@end




  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374

总结

我们在“M”中进行了数据的存储,在“V”中进行了UITextFielde和UIButton的创建添加,在“C”中实现了账号密码输入的正确与否的判断和登陆注册界面间的传值操作还有相应账号密码的保存。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/882404
推荐阅读
相关标签
  

闽ICP备14008679号