基于一些研发,我正在尝试为iOS实现MVVM。但是,无法初始化我的 View 模型属性。
这是我的问题:
我的 View Controller :
在viewDidLoad中:invViewModel = [[MVVMHomeInvViewModel alloc] init];
在“cellForRowAtIndexPath”(自定义单元格之一)中,
if(indexPath.row==2){...
UILabel *categoryLabel = [[UILabel alloc]init];
categoryLabel.text= invViewModel.categoryLabel;
…
}
和我的ViewModel:
.h:
@property (nonatomic,retain) NSString *categoryLabel;
.m:
@interface MVVMViewModel ()
// Private Access
@property (nonatomic, strong) MVVMModel *homeModel;
@end
@implementation MVVMHomeInvViewModel
- (instancetype)initWithHomeItems:(MVVMModel *)items{
self = [super init];
if (!self) return nil;
_categoryLabel = @"Categories";
return self;
}
@end
现在,标签“Catgeories”没有显示在我的模拟器中!!
谢谢您的帮助!
最佳答案
我意识到我的错。我的错!我没有调用初始化方法-ViewModel中的initWithHome项目,而是只是尝试使用通用的init方法访问它。
homeInvCaptureViewModel = [[MVVMHomeInvViewModel alloc] initWithHomeItems];
这样就解决了问题!
关于ios - 如何在MVVM iOS中初始化ViewModel属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26462230/