1. Traditional MVC

Apple’s MVC

3. My MVC

The design mode is not dead. It is necessary to make some changes according to your own needs and understanding, and any change is inevitable. The MVC used in the current project is actually slimming down the Controller. The image above is taken from the iOS Architecture Patterns, and HERE I’ll go straight to my implementation.

I’m going to break it down by module and I’m going to create MVC folders in the module directory, I don’t like big MVC file directories and I’m going to put the relevant classes in that folder.

Model

It’s the same thing you know about M in MVC

Swift // Test // // Created by Xia7 on 2017/9/19. // Copyright © 2017 Seven. All rights reserved. // import UIKit struct Model { var name: String? }Copy the code

View

I’m not going to write code like addSunViews in the Controller, so it’s all wrapped in the View, the Storyboard. I put the storyboard in the Controller out of personal habit, which I personally think belongs to the View

// // tableviewcell. swift // Test // // Created by Xiao7 on 2017/9/19. // Copyright © 2017 Seven. All rights reserved. // import UIKit class MyTableViewCell: UITableViewCell { @IBOutlet weak var label: UILabel! override funcawakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
Copy the code

Controller

The Controller contains Request and C->V value passing classes. In the actual project, part of the business logic code is placed in the ViewModel (the ViewModel is not the same as the MVVM, just a few ideas).

// // viewController.swift // Test // // Created by Xiao7 on 2017/9/19. // Copyright © 2017 Seven. All rights reserved. //  import UIKit class ViewController: UITableViewController { lazy private var mViewModel: ViewModel = ViewModel() lazy private var mRequest: Request = Request() override funcviewDidLoad() {
        super.viewDidLoad()
        mRequest.request {[weak self] inself? .tableView.reloadData() } } override funcdidReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mRequest.list.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell".for: indexPath) as! MyTableViewCell
        mViewModel.loadCellWithModel(mRequest.list[indexPath.row], view: cell)
        return cell
    }
}
Copy the code

ViewModel

We’re doing data transfer here

// // viewmodel. swift // Test // // Created by Xiao Seven on 2017/9/19. // Copyright © 2017 Seven. All rights reserved. // import UIKit class ViewModel: NSObject { func loadCellWithModel(_ model: Model, view: MyTableViewCell) { view.label.text = model.name } }Copy the code

Request

Network request encapsulation and Model storage, my Controller does not store the Model

// // request. swift // Test // // Created by Xiaoqion 2017/9/19. // Copyright © 2017 Seven. All rights reserved. // import  UIKit class Request: NSObject { var list: [Model] = [Model]() func request(result: () -> Void){for _ in0... 20 { var m = Model() m.name ="12345"
            list.append(m)
        }
        result()
    }
    
}
Copy the code

After the language

In fact, MVVM and MVP can and do solve the problem of C being too bloated. So why not use the popular MVVM, because the implementation of MVVM is dependent on RX and RAC, and they are not used in major projects so MVVM was not directly introduced (this is a really far-fetched excuse). “, and suggested that all third-party libraries had better be encapsulated by themselves, otherwise they will leave tears of regret when changing libraries.

Git addressgo go

Thanks for watching, here is my understanding of MVC in the project, I hope to make more corrections

reference

iOS Architecture Patterns