Last active
July 27, 2018 17:03
-
-
Save dmhts/f4a52056b98a3b03d48095e807616746 to your computer and use it in GitHub Desktop.
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
// MARK: Delegate approach | |
import UIKit | |
protocol ExampleViewModelDelegate: class { | |
func someEventDidOccur() | |
} | |
class ExampleViewModel { | |
weak var delegate: ExampleViewModelDelegate? | |
func someHandler() { | |
delegate?.someEventDidOccur() | |
} | |
} | |
class ExampleViewController : UIViewController { | |
var viewModel = ExampleViewModel() | |
public override func viewDidLoad() { | |
super.viewDidLoad() | |
viewModel.delegate = self | |
} | |
} | |
extension ExampleViewController: ExampleViewModelDelegate { | |
func someEventDidOccur() { | |
// handle the event | |
} | |
} | |
// MARK: Reactive approach | |
import UIKit | |
import ReactiveCocoa | |
class ExampleViewModel { | |
var someEvent: SignalProducer<Void, NoError> = SignalProducer { sink, _ in | |
sink.send(value: ()) | |
sink.sendCompleted() | |
} | |
} | |
class ExampleViewController : UIViewController { | |
var viewModel = ExampleViewModel() | |
public override func viewDidLoad() { | |
super.viewDidLoad() | |
viewModel.someEvent.startWithValues { | |
// handle the event | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment