Skip to content

Instantly share code, notes, and snippets.

@masamichiueta
Last active March 16, 2024 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masamichiueta/0054490ae0d5a28e45e79d53f198daa1 to your computer and use it in GitHub Desktop.
Save masamichiueta/0054490ae0d5a28e45e79d53f198daa1 to your computer and use it in GitHub Desktop.
iOSでMVVM(バインディングを使わない) ref: https://qiita.com/masamichiueta/items/3d09b98b80762bd6f248
class **ViewController: UIViewController {
var viewModel: **ViewModel!
...
override func viewDidLoad() {
super.viewDidLoad()
self.viewModel.stateDidUpdate = { [unowned self] state in
switch state {
case .state1:
....
case .state2:
....
}
}
}
}
class **ViewModel: ViewModel {
var stateDidUpdate: ((**ViewModelState) -> Void)?
...
func updateToState1() {
....
self.stateDidUpdate?(.state1)
}
}
enum **ViewModelState {
case state1, state2, ...
}
protocol ViewModel {
associatedtype State
var stateDidUpdate: ((State) -> Void)? { get set }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment