This file contains hidden or 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
extension ViewModelBindable where Self: AnyObject { | |
public var viewModel: ViewModel? { | |
get { | |
return getAssociatedObject(self, key: &AssociatedKey) | |
} | |
set(newViewModel) { | |
if let viewModel = newViewModel { |
This file contains hidden or 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
extension ViewModelBindable where Self: AnyObject { | |
public var viewModel: ViewModel? { | |
// store view model | |
} | |
} |
This file contains hidden or 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
public protocol ViewModelBindable: class { | |
typealias ViewModel | |
var viewModel: ViewModel? { get set } | |
func bindViewModel(viewModel: ViewModel) | |
func unbindViewModel(viewModel: ViewModel) |
This file contains hidden or 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
public class MyViewController: UIViewController { | |
var viewModel: MyViewModel? { | |
willSet(maybeViewModel) { | |
if let _ = self.viewModel { | |
unbindViewModel() | |
} | |
} | |
didSet { | |
if isViewLoaded(), let viewModel = self.viewModel { |
NewerOlder