我在我的应用程序中使用 ReactiveCocoa 框架来获得使用 MVVM 设计模式的强大功能。
所以对于每个控制器,我都有一个 ViewModel。并且Controller绑定到他的ViewModel。
UIButton 绑定将如下所示:
@implementation HomeController
-(void) bindViewModel {
self.viewHeader.buttonSide.rac_command = self.viewModel.execiteFullPortfolio;
}
一切正常,但是当我想将参数传递给 ViewModel 时,我不确定这样做的正确方法是什么......
假设我有一个股票的 UICollectionView,每次点击特定股票,我都想导航到该股票资料页面。该逻辑应该在 ViewModel 上完成,但是我如何通过 RACCommand 获取库存?
我目前正在做的是:
@implementation HomeController
-(void) bindViewModel {
__unsafe_unretained HomeController *weakSelf = self;
self.viewPortfolioPusherView.scrollGainView.blockSelect = ^ (STStock *stock){
weakSelf.viewModel.selectedStock = stock;
[weakSelf.viewModel.executeGoToStock execute:[RACSignal empty]];
};
}
@implementation HomeViewModel
-(void) initialization {
self.executeGoToStock = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf moveToSelectedStock];
});
return [RACSignal empty];
}];
}
-(void) moveToSelectedStock {
[self stockProfileControllerLazy];
self.stockProfileController.stock = self.selectedStock;
[Navigator pushController:self.stockProfileController fromController:[self.delegate controller]];
}
我确定这不是最佳做法!所以我问,什么是??
谢谢 。