I cannot understand, why my View
doesn't update after ViewModel
changes.
I have binded my button properties in View
:
- (void)bindViewModel
{
// ViewModel -> View
RAC(self.nextButton, backgroundColor) = RACObserve(self.viewModel, nextButtonColor);
RAC(self.nextButton, enabled) = RACObserve(self.viewModel, nextButtonEnabled);
}
This is my ViewModel
:
@implementation REBaseAuthViewModel
- (instancetype)init
{
self = [super init];
if ( self ) {
RACSignal *inputValidSignal = [RACObserve(self, textFieldText)
map:^id(NSString *text) {
BOOL b = [self validateInput:text];
NSLog(@"Input is valid: %d", b);
return @(b);
}];
RACSignal *inProressSignal = RACObserve(self, inProress);
RAC(self, nextButtonEnabled) = [[inputValidSignal
combineLatestWith:[inProressSignal not]]
and];
}
return self;
}
- (BOOL)validateInput:(NSString *)text
{
return text.length > 0;
}
- (void)setNextButtonEnabled:(BOOL)nextButtonEnabled
{
_nextButtonEnabled = nextButtonEnabled;
NSLog(@"nextButtonEnabled: %d", nextButtonEnabled);
self.nextButtonColor = nextButtonEnabled ? [UIColor primaryBlueColor] : [UIColor inactiveButtonColor];
}
@end
Log shows that nextButtonEnabled
is changing correctly in ViewModel
. But View
doesn't change.
答案 0 :(得分:1)
发现错误。我的viewModel
是零。
我从另一个继承我的控制器。我在ViewModel
中初始化ViewDidLoad
,bindViewModel
之后调用bindViewModel
。因为ViewDidLoad
在父控制器的viewModel
中被调用。
现在我在initWithCoder
中初始化ViewDidLoad
,而不是- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if ( self ) {
self.viewModel = [[REPhoneViewModel alloc] init];
}
return self;
}
/*- (void)viewDidLoad
{
[super viewDidLoad];
self.viewModel = [[REPhoneViewModel alloc] init];
}*/
:
SELECT *
FROM [EMAIL].[dbo].[T_client]
JOIN [EMAIL].[dbo].[T_promo_code] ON
(INDEX([EMAIL].[dbo].[T_client].idx_client)) = (INDEX ([EMAIL].[dbo].[T_promo_code].idx_code))