View Model Binding

RudinP·5일 전
0

Study

목록 보기
384/384

  • MVVM 패턴에서는 뷰모델을 뷰컨트롤러의 속성으로 추가한다.
  • 뷰 모델 - 뷰 간 바인딩한다.
  • 뷰컨트롤러당 뷰모델 1개 존재

ViewModelBindableType

import UIKit

//VC에서 이 프로토콜을 채용
protocol ViewModelBindableType {
    associatedtype ViewModelType //제네릭 사용
    
    var viewModel: ViewModelType! { get set }
    func bindViewModel()
}

extension ViewModelBindableType where Self: UIViewController {
    mutating func bind(viewModel: Self.ViewModelType) {
        //뷰컨트롤러에 추가된 뷰모델 속성에 파라미터로 전달된 실제 뷰 모델 저장
        //뷰컨트롤러에서 bindViewModel을 직접 호출하지 않아도 됨
        self.viewModel = viewModel
        loadViewIfNeeded()
        
        bindViewModel()
    }
}

ViewController

import UIKit

class MemoListViewController: UIViewController, ViewModelBindableType {
    
    var viewModel: MemoListViewModel!
    
    func bindViewModel() {
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
profile
iOS 개발자가 되기 위한 스터디룸/스터디의 레퍼런스는 모두 kxcoding

0개의 댓글