Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Reverse ViewModel
// This is an experiment trying to understand how a reverse view model can be coded
// The inputView takes several inputs from the user and sends these to a delegate for further processing upon tapping a button
// the view model transforms the input strings to the correct type needed for the processing
// Approach inspired by
// this struct should really be in the model, but is added here for brevity
struct Parameters {
var string1: String
var double1: Double
var double2: Double
var int1: Int
protocol InputViewDelegate: class {
func process(_ params: Parameters?)
class InputView: UIView {
@IBOutlet private weak var textView1: UITextView!
@IBOutlet private weak var doubleTextField1: UITextField!
@IBOutlet private weak var doubleTextField2: UITextField!
@IBOutlet private weak var intTextField1: UITextField!
var viewModel = ViewModel()
weak var delegate: InputViewDelegate?
@IBAction func processButtonTapped(_ sender: UIButton) {
viewModel.string1 = self.textView1.text
viewModel.doubleString1 = self.doubleTextField1.text!
viewModel.doubleString2 = self.doubleTextField2.text!
viewModel.intString1 = self.intTextField1.text!
extension InputView {
struct ViewModel {
let string1: String
let doubleString1: String
let doubleString2: String
let intString1: String
extension InputView.ViewModel {
func parameters() -> Parameters
let params = Parameters(string1: self.string1,
double1: Double(self.double1)!,
double2: Double(self.double2)!,
int1: Int(self.int1)!)
return params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment