【问题标题】:How to initialise ViewModel properties in MVVM iOS如何在 MVVM iOS 中初始化 ViewModel 属性
【发布时间】:2014-12-15 05:36:21
【问题描述】:

基于一些研发,我正在尝试为 iOS 实现 MVVM。但是,无法初始化我的视图模型属性。

这是我的问题:

我的视图控制器:

在 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”。!!

感谢您的帮助!

【问题讨论】:

  • 我对超过 1k 的代表感到惊讶,因为您无法在问题中正确格式化代码。如果你这样做,它会更容易阅读。
  • 哎呀!对不起。我只是点击Enter并匆忙提交。我将来肯定会tc。当我意识到时,@nickfalk ahd 已经完成了我的工作。谢谢!

标签: ios model-view-controller mvvm


【解决方案1】:

我意识到我的错。我的错!我没有在 ViewModel 中调用 initilize 方法 - initWithHomeItems,而是简单地尝试使用通用 init 方法访问它。

 homeInvCaptureViewModel = [[MVVMHomeInvViewModel alloc] initWithHomeItems];  

这解决了问题!

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2013-11-02
    • 2021-01-09
    • 1970-01-01
    相关资源
    最近更新 更多