Last active
March 16, 2024 11:46
-
-
Save masamichiueta/0054490ae0d5a28e45e79d53f198daa1 to your computer and use it in GitHub Desktop.
iOSでMVVM(バインディングを使わない) ref: https://qiita.com/masamichiueta/items/3d09b98b80762bd6f248
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class **ViewController: UIViewController { | |
var viewModel: **ViewModel! | |
... | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.viewModel.stateDidUpdate = { [unowned self] state in | |
switch state { | |
case .state1: | |
.... | |
case .state2: | |
.... | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class **ViewModel: ViewModel { | |
var stateDidUpdate: ((**ViewModelState) -> Void)? | |
... | |
func updateToState1() { | |
.... | |
self.stateDidUpdate?(.state1) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum **ViewModelState { | |
case state1, state2, ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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