2016-12-14 5 views
1

Als Followup zu this Frage, die this fantastic answer empfangen und mit dem folgenden Beispiel ...ReactiveSwift - Aktualisieren des Werts von MutableProperty?

class Model { 
    let mapType = MutableProperty<MKMapType>(.standard) 
} 

class SomeViewController: UIViewController { 

    let viewModel: Model 
    var mapView: MKMapView! 

    init(viewModel: Model) { 
     self.viewModel = viewModel 
     super.init(nibName: nil, bundle: nil) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     mapView = // Create the map view 
     viewModel.mapType.producer.startWithValues { [weak self] mapType in 
      self?.mapView.mapType = mapType 
     } 
     // Rest of bindings 
    } 
    // The rest of the implementation... 
} 

class SomeOtherViewController: UIViewController { 

    let viewModel: Model 
    var segmentedControl: UISegmentedControl! 

    init(viewModel: Model) { 
     self.viewModel = viewModel 
     super.init(nibName: nil, bundle: nil) 
     // Pretend we've setup the segmentedControl, and set its action to `updateMap(sender:)`... 
     // The rest of the initialization... 
    } 

    func updateMap(sender: UISegmentedControl) { 
     let newMapType =... 
     self.viewModel.mapType = newMapType // How do I update this value? 
    } 
    // The rest of the implementation... 
} 

Wie ich den Wert von Model.mapType aktualisieren können, und haben seine Änderungen an den Beobachter geschickt (wie SomeViewController in der Beispiel)?

Antwort

2

Sie müssen nur die value-Eigenschaft festlegen. Dadurch werden Änderungen ausgelöst, die automatisch an die Beobachter gesendet werden.

self.viewModel.mapType.value = newMapType

+0

Wow ... Ich konnte ich das versucht, genau das habe geschworen. * facepalm * Ich schätze die schnelle Antwort! – forgot

+0

Könnte auch geschworen haben Ich markierte dies ist die Ausnahme Antwort ..... Fixed, und nochmals vielen Dank! – forgot

Verwandte Themen