- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在OC中做过这样的:
#import "ViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
@interface ViewController () <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
}
- (void)initUI {
__weak typeof(self) weakSelf = self;
[[RACObserve(self.btn, enabled) map:^id(id value) {
return [value boolValue] ? [UIColor greenColor] : [UIColor redColor];
}] subscribeNext:^(id x) {
[weakSelf.btn setTitleColor:x forState:UIControlStateNormal];
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.btn.enabled = !self.btn.enabled;
}
@end
现在,我尝试使用最新版本的 ReactiveCocoa 在 swift 中实现相同的目标,该怎么做?
最佳答案
ReactiveCocoa 5.0 will add UIKit Extensions .
button.reactive.values(forKeyPath:)
这在一个简单的 Playground 中对我有用:
button.reactive.values(forKeyPath: "enabled")
.map { $0 as? Bool }
.skipNil()
.map { enabled in enabled ? UIColor.blue : UIColor.red }
.startWithValues { [weak button = button] color in
button?.setTitleColor(color, for: .normal)
}
然而,这是不被鼓励的,因为
在这个例子中,一个简单的 MutableProperty
被用作模型,它可以在 ViewModel 或任何地方
let buttonEnabled = MutableProperty<Bool>(false)
button.reactive.isEnabled <~ buttonEnabled
buttonEnabled.producer
.map { enabled in enabled ? UIColor.blue : UIColor.red }
.startWithValues { [weak button = button] color in
button?.setTitleColor(color, for: .normal)
}
不幸的是,您不能像使用 isEnabled
关于objective-c - ReactiveCocoa 5.0 中的 RACObserve(object, keyPath),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40711642/
我使用 RACObserve 创建了一个信号,但它不能很好地工作,而 rac_textSignal 可以。这是我的代码: [RACObserve(self, searchText.text); sub
我想知道为什么下面的代码不起作用: RACDisposable *subscr; subscr = [[[RACObserve(self.myTextView, text) filter:^B
我试图在我的 ViewModel 中观察一个属性,然后使用 ReactiveCocoa 更新一个带有它的值的标签,但它没有更新。 这是我得到的: View 模型 var amount: NSDecim
我有 RACObserve block ,我想重试调用直到它成功返回。所以它不会显示错误消息,但会重试获取。感谢您的帮助! [[[[RACObserve(self, currentLocati
考虑我在我的 View Controller 中,我添加了 Singleton 属性的 RACObserve,并且在 subscribNext 里面我有一个自我引用。 代码如下: [RACObserv
我在对 NSMutableArray 中的值更改使用react时遇到问题。 我有以下(有些简化的)代码来检测更改: [[RACObserve(self, postedImagesIds) filter
我正在观察类的属性每 0.2 秒更改一次。我想将观察延迟到每秒。 我尝试过: RACObserve(object, keyPath: "attribute").delay(1).subscribeNe
下面的代码工作正常: [RACObserve(self.person, firstName) subscribeNext:^(id x) { self.descriptionText =
我创建了一个信号来观察 -viewDidLoad 中文本字段框架的宽度: RACSignal *destinationDisplayWidthSignal = [RACObserve(self
这个新的现有项目正在使用 Objective-C 代码和 RACObserve(Reactive cocoa ) 来读取 api 响应。我想将 obj-c 转换为 swift 。 当前的 Obj-c
创建一个类的实例之后。我通过 RAC 宏绑定(bind)一些属性。 _theTable = [[ScrollableTable alloc] initWithFrame:CGRectMake(0, 0
dynamic var categories: [Category]? dynamic var productViewModels: [ProductViewModel]? var totalCate
作为 ReactiveCocoa 的新手,我希望得到一些建议: 我正在尝试创建一个包含多个从 XML 文件解析的 Field 对象的动态表单。每个 Field 都可以有多个验证规则,这些规则将针对 F
我正在使用 react 性 cocoa 从服务器创建下载文件。我有一个带有进度属性(进度值、currentBytes、totalBytes)的 DownloadMapFileOperation.m 文
我想监听 UIButton.enabled 属性来改变 button.titleColor 我在OC中做过这样的: #import "ViewController.h" #import @inter
我是响应式编程的新手。我已经阅读了 Reactive Cocoa 的文档,但没有意识到 RACAble()、RACObserve() 和 RACBind() 之间的区别。 请通过一些示例代码片段帮助我
我是一名优秀的程序员,十分优秀!