gpt4 book ai didi

swift3 - ReactiveSwift - 更新 MutableProperty 的值?

转载 作者:行者123 更新时间:2023-12-02 08:17:05 32 4
gpt4 key购买 nike

作为 this 的后续问题,收到 this fantastic answer ,并使用以下示例...

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...
}

如何更新 Model.mapType 的值,并将其更改发送给任何观察者(如示例中的 SomeViewController)?

最佳答案

你只需要设置它的value属性,这将触发自动发送给观察者的更改。
self.viewModel.mapType.value = newMapType

关于swift3 - ReactiveSwift - 更新 MutableProperty 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41135196/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com