2
我正在制作一个简单的iOS应用程序来学习ReactiveCocoa。直到现在我一直在使用XIB文件,但决定切换到故事板。 在我的第一个视图中,我有登录屏幕,当用户按下按钮时,viewModel执行RACCommand来认证用户并下载他的联系人列表。之后,我需要从ViewController调用performSegueWithIdentifier:
来显示下载的数据。如何在ViewController中通知viewModel成功完成其操作?ViewModel完成操作时在ViewController中的通知
下面是从视图控制器代码片段:
RAC(self.viewModel, username) = self.usernameTextField.rac_textSignal;
RAC(self.viewModel, password) = self.passwordTextField.rac_textSignal;
self.loginButton.rac_command = self.viewModel.executeSignin;
,并从其视图模型片段:
////////////////////////////////IN INIT////////////////////////////////////
self.executeSignin =
[[RACCommand alloc] initWithEnabled:validAuthenticateSignal
signalBlock:^RACSignal *(id input) {
return [self executeSigninSignal];
}];
//////////////////////////////////////////////////////////////////////////
-(RACSignal *)executpsigninsignal {
return [[[self.services getAuthenticationService]
authenticationSignalFor:self.username andPassword:self.password]
//Return user if exists
flattenMap:^RACStream *(STUser *user) {
return [[[[[self services] getContactsLoadService]
contactsLoadSignalForUser:user] deliverOn:[RACScheduler mainThreadScheduler]]
//Return user contacts
doNext:^(NSArray *contacts) {
_downloadedContacts = [NSArray arrayWithArray:contacts];
}];
}];
}
我也试图观察的ViewController的ViewModels downloadedContacts属性:
RACSignal *contactsLoadSignal = RACObserve(self.viewModel, downloadedContacts);
[[contactsLoadSignal filter:^BOOL(NSArray *value) {
return value!=nil && value.count>0;
}]subscribeNext:^(NSArray *array) {
[self performSegueWithIdentifier:@"pushContactsList" sender:self];
}];
但这似乎不起作用,并不真正看起来不错。