0

The following code works:

import UIKit
import ReactiveSwift
import ReactiveCocoa

class ViewController: UIViewController {
    let myAction = Action<UIButton, (), Never> { _ in
        return SignalProducer { observer, _ in
                observer.send(value: ())
                observer.sendCompleted()
            }
    }
    
    let aProp = MutableProperty<String>("MutablePropertyStart")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myAction.events.observeValues { [unowned self]_ in
            self.aProp.value = "MutablePropertyChanged"
        }
     
        view.backgroundColor = .black
    
        let button = UIButton()
        button.frame = CGRect(x:100, y:100, width:100, height:100)
        button.setTitle("Press Me", for: .normal)
        view.addSubview(button)
        button.reactive.pressed = CocoaAction(self.myAction) { sender in
            return sender
        }
        
        let label = UILabel()
        label.frame = CGRect(x:100, y:200, width:200, height: 100)
        label.text = "LabelStart"
        label.textColor = .white
        view.addSubview(label)
        
        label.reactive.text <~ aProp
    }
}

But is there a more correct way with less code? i.e. can I make the button action change the property directly without the need for this other SignalProducer inside myAction?

Andrew Arrow
  • 4,248
  • 9
  • 53
  • 80

1 Answers1

0

If you’re just trying to observe button presses, I think using the <~ operator and controlEvents for ReactiveCocoa might make this a little tidier

self.aProp <~ button.reactive.controlEvents(.primaryActionTriggered)
    .map { _ in 
        "MutablePropertyChanged"
    }