2016-04-25 43 views
0
RAC(self.balanceLabel,text) = RACObserve(self.viewModel, balance); // balanceLabel is a UILabel,that is ofcourse work 
RAC(self.supplierNameButton.titleLabel.text) = RACObserve(self.viewModel, supplierName); // that is not work,i think this is I don't use the - setTitle:forState: method 

我的问题是:我该如何使用Reactivecocoa视图模型的供应商结合其UIControlStateNormal的UIButon文本Reactivecocoa结合的UIButton的标题

回答

0

标题需要订阅块被更新

@weakify(self) 
[RACObserve(self.viewModel, supplierName) 
    subscribeNext:^(NSString *supplierName) { 
     @strongify(self) 
     [self.supplierNameButton setTitle:supplierName forState:UIControlStateNormal]; 
    }]; 

通过订阅移动到一个类中的方法上的UIButton

- (void)setTitleSignal:(RACSignal *)titleSignal forState:(UIControlState)state { 
    @weakify(self) 
    [titleSignal subscribeNext:^(NSString *title) { 
     @strongify(self) 
     [self setTitle:title forState:state]; 
    }]; 

}

的结合可以表达一点清洁剂作为

[self.supplierNameButton setTitleSignal:RACObserve(self.viewMode, supplierName) forState:UIControlStateNormal]; 
1

遗憾的是,只有绑定的属性和按钮的文字只能使用setTitle:forState方法来更新工作。

但是有一个解决办法:你可以使用rac_liftSelector:withSignalsFromArray方法,它是有用的,当你不想惹订阅(和weakify/strongify):

[self.supplierNameButtonButton rac_liftSelector:@selector(setTitle:forState:) withSignalsFromArray:@[RACObserve(self.viewModel, supplierName), [RACSignal return:@(UIControlStateNormal)]]];