![](/img/trans.png)
[英]What is the correct way to auto expand a UIButton to the width of an interface on orientation change
[英]What is correct way to make a UIButton change a MutableProperty?
以下代碼有效:
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
}
}
但是有沒有更正確的方法和更少的代碼? 即,我可以讓按鈕操作直接更改屬性,而不需要 myAction 中的其他 SignalProducer 嗎?
如果您只是想觀察按鈕按下,我認為對 ReactiveCocoa 使用<~
運算符和 controlEvents 可能會使這更整潔
self.aProp <~ button.reactive.controlEvents(.primaryActionTriggered)
.map { _ in
"MutablePropertyChanged"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.