iOS Initial Knowledge of MVC Mode - (Containing Simple Login Interface)

MVC mode

The full name of MVC is Model View Controller. It is the abbreviation of Model View Controller, which separates the input, processing and output of the program. It consists of three parts. The functions are as follows.

Model Data Processing Layer, including Network Request, Data Processing
View (View) can see the interface, providing users with display information
Controller (Controller) acts as a bridge between view and model, which can reduce the coupling between view and model, and make the authority of view and model clearer, so as to improve the development efficiency.
Interaction between layers

1. Controller can communicate with View. Controller can control View through outlet. View can communicate with Controller through delegate, data source, target-action.
2. Controller receives the interactive event (eg:Button click event) from View and handles it with Model after judgment. Controller uses API for Model.
3. After processing the data, the Model will inform Controller by notification or kvo if necessary. The Model is only responsible for notification, and the subsequent operation is decided by Controller.
4. No communication between Model and View

How to go to MVC mode and tell yourself that iOS controller is not UIViewController, but a common Controller, without View, module partition, each module corresponds to its own View

Simple landing interface

Design sketch
Simple function: default account 123 password 789, enter the account password, and is the default, click login, enter another blank view, if the account password error, then pop up a prompt box.

- Overview
First of all, three group s are called Model, View and Controller. As the name implies, model related files are put in Model, View related files are put in View, and ViewController related files are put in ViewController. Then, TextFiled and Button related controls of landing pages previously written in ViewController are written in custom View and added. Add button event, through proxy callback, pass the input account number and password to Model in Controller, compare the input account password in Model, and then notify Controller of the result of the comparison through Notification, add the Button proxy in Controller to realize the click button event.

  • First set up the group
    Create three group s named M, V, C, and create related files, because ViewController itself, so drag the original to C file directly.
    As shown in the figure, the file name starts at will.
  • Code
    In LandView
    Controls that store only interface display, UITextField and UIButton
#import <UIKit/UIKit.h>

@protocol PassButtonDelegate <NSObject>

-(void)clickButton: (UIButton *)sender;


@interface LandView : UIView
@property (nonatomic, strong) UITextField *accountTextField;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UIButton *landButton;
//@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, weak) id<PassButtonDelegate> delegate;




#import "LandView.h"

@implementation LandView

- (void)viewInit {
    _accountTextField = [[UITextField alloc] init];
    [_accountTextField setFrame:CGRectMake(100, 100, 200, 40)];
    _accountTextField.layer.borderColor = [UIColor blackColor].CGColor;
    _accountTextField.placeholder = @"Please enter your account number.";
    _accountTextField.layer.borderWidth = 1;
    [self addSubview:_accountTextField];
    _passwordTextField = [[UITextField alloc] init];
    [_passwordTextField setFrame:CGRectMake(100, 160, 200, 40)];
    _passwordTextField.layer.borderColor = [UIColor blackColor].CGColor;
    _passwordTextField.placeholder = @"Please input a password";
    _passwordTextField.secureTextEntry = YES;
    _passwordTextField.layer.borderWidth = 1;
    [self addSubview:_passwordTextField];
    _landButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_landButton setFrame:CGRectMake(140, 220, 80, 40)];
    [_landButton setTitle:@"Land" forState:UIControlStateNormal];
    [_landButton setTintColor:[UIColor blackColor]];
    [_landButton addTarget:self action:@selector(pressLand:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_landButton];


-(void)pressLand:(UIButton *)sender {
    //Callback through proxy
    if ([_delegate respondsToSelector:@selector(clickButton:)]) {
        [_delegate clickButton:sender];

In LandModel
Model is mainly used for data processing, where the password of the account is compared.


#import <Foundation/Foundation.h>


@interface LandModel : NSObject
-(void)checkNames:(NSString *)username withPassword:(NSString *)password;



#import "LandModel.h"

@implementation LandModel
-(void)checkNames:(NSString *)username withPassword:(NSString *)password {
    if ([username isEqualToString:@"123"] && [password isEqualToString:@"789"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"landSuccessful" object:self];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"landFail" object:self];

Notification informs ViewController what to do
Two notifications use Name, the "landSuccessful" in the example, to determine which notification was received.

In ViewController


#import <UIKit/UIKit.h>
#import "LandModel.h"
#import "LandView.h"

@interface ViewController : UIViewController <PassButtonDelegate>
@property (nonatomic, strong) LandView *landView;
@property (nonatomic, strong) LandModel *mModel;



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _landView = [[LandView alloc] initWithFrame:CGRectMake(0, 0, W, H)];
    [_landView viewInit];
    //Setting agent
    _landView.delegate = self;
    [self.view addSubview:_landView];
    _mModel = [[LandModel alloc] init];
    //Add a notification method to execute the corresponding method when the same notification as name is received
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(landSuccessful:) name:@"landSuccessful" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(landFail:) name:@"landFail" object:nil];

//Agent method
- (void)clickButton:(UIButton *)sender {
    [_mModel checkNames:_landView.accountTextField.text withPassword:_landView.passwordTextField.text];

//The method executed after matching the account password goes to a blank page
-(void)landSuccessful:(NSNotification *)notification {
    SuccessfulViewController *succcessfulViewController = [[SuccessfulViewController alloc] init];
    succcessfulViewController.view.backgroundColor = [UIColor whiteColor];
    [self presentViewController:succcessfulViewController animated:NO completion:nil];

//Account Password Matching Failed, Executed Method
-(void)landFail:(NSNotification *)notification {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"error" message:@"Error in account or password entry" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *suretAction = [UIAlertAction actionWithTitle:@"confirm" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:suretAction];
    [self presentViewController:alertController animated:YES completion:nil];

//Keyboard recovery
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];

demoSimple landing page

Keywords: network iOS

Added by ILYAS415 on Wed, 18 Sep 2019 15:16:02 +0300