美文网首页
iOS MVVM 一分钟教程(无RAC, Swift版)

iOS MVVM 一分钟教程(无RAC, Swift版)

作者: 国服最坑开发 | 来源:发表于2017-06-23 17:37 被阅读0次

    这是MVVM基础用法,
    相比MVP, 最大的不同点在于, MVVM要负责对V的更新

    Model

    // Person.swift
    struct Person {
        let firstName: String
        let lastName: String
    }
    

    ViewModel

    // GreetinViewModel.swift
    
    protocol GreetingViewModelProtocol {
        var greeting: String? {get}
        var greetingDidChange:((GreetingViewModelProtocol)->())? { get set }
        init(person: Person)
        func showGreeting()
    }
    
    class GreetingViewModel : GreetingViewModelProtocol {
        let person: Person
        
        required init(person: Person) {
            self.person = person
        }
    
        var greeting: String? {
            didSet {
                // VM的值被更新后, 再去更新V
                self.greetingDidChange!(self)
            }
        }
    
        var greetingDidChange: ((GreetingViewModelProtocol) -> ())?
        
        func showGreeting() {
            self.greeting = "Hello  \(person.firstName) \(person.lastName)"
        }
    
    }
    

    View

    // ViewController.swift
    
    
    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var lbTitle: UILabel!
        
        var person:Person? = nil
        
        var viewModel:GreetingViewModelProtocol! {
            didSet {
                // 负责VM更新V
                self.viewModel.greetingDidChange = { vm in
                    self.lbTitle.text = vm.greeting
                }
            }
        }
        
        @IBAction func onClick(_ sender: Any) {
            // V更新VM
            self.viewModel.showGreeting()
        }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            person = Person(firstName: "Gao", lastName: "L")
            self.viewModel = GreetingViewModel(person: person!)
        }
    
    }
    

    工程代码

    相关文章

      网友评论

          本文标题:iOS MVVM 一分钟教程(无RAC, Swift版)

          本文链接:https://www.haomeiwen.com/subject/hagccxtx.html