gpt4 book ai didi

ios - ReactiveCocoa : View doesn't update when ViewModel property changes

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:48 26 4
gpt4 key购买 nike

我无法理解,为什么我的 ViewViewModel 更改后没有更新。

我已经在 View 中绑定(bind)了我的按钮属性:

- (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

日志显示 nextButtonEnabledViewModel 中正确更改。但是 View 没有改变。

最佳答案

发现错误。我的 viewModel 是零。

我从另一个人那里继承了我的 Controller 。而我在ViewDidLoad中初始化了ViewModel,在bindViewModel之后调用。因为在父 Controller 的 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];
}*/

关于ios - ReactiveCocoa : View doesn't update when ViewModel property changes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44458221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com