ios - 如何在MVVM iOS中初始化ViewModel属性

标签 ios model-view-controller mvvm

基于一些研发,我正在尝试为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/

相关文章:

c# - 如何从 dll 中反编译/提取 (cshtml--(razor view))

java - MVC 游戏设计 Java

Silverlight MVVM 和处理 FOCUS

c# - 使用WPF和MVVM的多选

wpf - MVVM 中的简单事件处理

iphone - iOS 以编程方式获取物理屏幕尺寸?

ios - 如何在 ios 中将 Google map 标记从一个位置平滑地动画化到另一个位置

php - magento 价格含税

iphone - 适用于 iOS 应用程序的分析库

ios - 如何在 swift 中动画绘制 UIBezierPath