ReactiveCocoa:当 ViewModel 属性 更改时,视图不会更新
ReactiveCocoa: View doesn't update when ViewModel property changes
我不明白,为什么我的 View
在 ViewModel
更改后没有更新。
我已经在 View
:
中绑定了我的按钮属性
- (void)bindViewModel
{
// ViewModel -> View
RAC(self.nextButton, backgroundColor) = RACObserve(self.viewModel, nextButtonColor);
RAC(self.nextButton, enabled) = RACObserve(self.viewModel, nextButtonEnabled);
}
这是我的 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
日志显示 nextButtonEnabled
在 ViewModel
中正确更改。但是 View
没有改变。
发现错误。我的 viewModel
是零。
我从另一个人那里继承了我的控制器。而我在ViewDidLoad
中初始化了ViewModel
,在bindViewModel
之后调用。因为在父控制器的ViewDidLoad
中调用了bindViewModel
。
现在我在 initWithCoder
中初始化 viewModel
,而不是 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];
}*/
我不明白,为什么我的 View
在 ViewModel
更改后没有更新。
我已经在 View
:
- (void)bindViewModel
{
// ViewModel -> View
RAC(self.nextButton, backgroundColor) = RACObserve(self.viewModel, nextButtonColor);
RAC(self.nextButton, enabled) = RACObserve(self.viewModel, nextButtonEnabled);
}
这是我的 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
日志显示 nextButtonEnabled
在 ViewModel
中正确更改。但是 View
没有改变。
发现错误。我的 viewModel
是零。
我从另一个人那里继承了我的控制器。而我在ViewDidLoad
中初始化了ViewModel
,在bindViewModel
之后调用。因为在父控制器的ViewDidLoad
中调用了bindViewModel
。
现在我在 initWithCoder
中初始化 viewModel
,而不是 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];
}*/