Objective-C 中的双重/双向数据绑定使用`TJBinder`

Dual / Bidireactional data binding in Objective-C using `TJBinder `

在写得很好的 TJBinder 的帮助下,我能够将视图挂接到 DTO 或模型 class。

根据示例,

    // Retrieve your data object from an external resource here
    Fruit* fruit = [Fruit new];
    fruit.name = @"apple";
    fruit.color = [UIColor redColor];

    // Tell the root view what is its data object
    self.view.dataObject = fruit;

但是现在,当有 UI 更新时说 UITextField 已更新,那么我如何将其绑定到模型 class 中?

到目前为止我试过了,

    //Now say user typed "orange" instead of "apple" in a text field.
    //After a button action
    fruit = self.view.dataObject;
    NSLog(@"%@", fruit.name); //-> returns old object itself. DTO not updated after changing view.

非常感谢任何帮助!

目前TJBinderupdateView可以交流

模型 — 到 — > 视图

实现

视图 — 至 — > 模型

我在“TJBinder.h”

中添加了额外的方法 updateModel

此方法将遍历每个 TJBindEntry 对象并设置取自视图关键路径的模型对象值。

-(void)updateModel{
    
    for (TJBindEntry* entry in self.bindings)
    {
        [self updateBindingEntryForView:entry];
    }
}

-(void) updateBindingEntryForView:(TJBindEntry*)entry
{
 
    id extractedValue = [entry.view valueForKey:entry.viewKey];
    
    [self.dataObject setValue:extractedValue forKey:entry.dataObjectKeyPath];
}

Refer here is complete code in my Gist