有没有办法自动将UITextField绑定到我的数据模型中的变量? [英] Is there a way to automatically tie a UITextField to a variable in my data model?

查看:56
本文介绍了有没有办法自动将UITextField绑定到我的数据模型中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个视图控制器,我以编程方式将一堆 UITextField 添加到我的视图中。我想用一些我从CoreData加载的文本预先填充文本字段,但允许用户进入并根据需要更改该文本。然后我需要返回并将该新文本再次保存回CoreData,并使用新值执行计算。保存和加载本身不是问题,但我正在寻找一种跟踪用户更改内容的好方法。

I'm building up a view controller where I'm adding a bunch of UITextFields to my view programmatically. I want to pre-populate the textfields with some text that I'm loading from CoreData, but allow the user to go in and change that text if they want. I then need to go back and save that new text back to CoreData again, as well as perform calculations using the new value. The saving and loading itself isn't a problem, but I'm looking for a good way to track what the user has changed.

过去,我用 .tag 属性完成了这项工作UITextField textFieldDidEndEditing:委托方法。问题是我将在这个视图中有几十个文本域 - 足以让我真的不记得 .tag 属于我模型中的变量。我必须回过头来查看我在创建它时分配的标签,这将是一种痛苦,而整个事情确实很容易出错。另外,我将在多个不同的循环中创建文本字段,因此在创建它时为每个文本字段获取唯一标记将很困难(可能不可能?)。

In the past, I've done this with the .tag property of the UITextField and the textFieldDidEndEditing: delegate method. The problem is I'm going to have dozens of textfields in this view - enough so that I can't really remember what .tag belongs to what variable in my model. Having to go back to look up what tag I assigned when I created it is going to be a pain, and the whole thing feels really error prone. Additionally, I'm going to be creating the textfields in multiple different loops, so getting a unique tag for each textfield when I create it will be difficult (maybe impossible?).

我正在寻找的是一种在我创建它时将UITextField绑定到模型中的变量的方法,并且从那时起就知道,每当用户更新该文本字段时,我在模型中指定的值将是立即更新以用于将来的计算,并在用户离开屏幕时保存回CoreData。有没有办法做到这一点?

What I'm looking for is a way to "tie" the UITextField to a variable in my model when I create it and just know from that point on, whenever the user updates that textfield, the value I specify in my model will be instantly updated to be used in future calculations, and saving back to CoreData when the user leaves the screen. Is there a way to do this?

编辑1:另外需要注意的是我的模型本身有点复杂。我有几个不同的自定义 NSObject 子类,它们都包含我的数据的不同部分。某些对象具有本身是其他自定义对象的属性。我也有字典,其中值将是某些自定义对象的实例。

EDIT 1: Another thing to note is that my model itself is kinda complex. I have several different custom NSObject subclasses that are all holding different pieces of my data. Some of the objects have properties that are themselves other custom objects. I also have dictionaries where the values will be instances of some of the custom objects.

例如,我可能有这些对象:

As an example, I might have these objects:


  • MySmallObject1 ,其属性 name size value (所有 NSStrings

  • MySmallObject2 ,其属性日期成本 NSDate NSNumber

  • MyBigObject ,其属性 box1 box2 MySmallObject1 分别是MySmallObject2 实例)

  • theDict ,这是 NSDictionary 具有 MyBigObject s作为值的实例

  • MySmallObject1, which has properties name,size, and value (all NSStrings)
  • MySmallObject2, which has properties date and cost (an NSDate and an NSNumber)
  • MyBigObject, which has properties box1 and box2 (which are MySmallObject1 and MySmallObject2 instances, respectively)
  • theDict, which is an NSDictionary instance that has MyBigObjects as values

所以当我正在构建我的文本域,我可能实际上是在一棵树上看起来像:

So when I am building my textfields, I might actually be going down a tree that would look something like:

for (NSString *key in [theDict allKeys])
{
    UITextField *txt = [[UITextField alloc] initWithFrame:...];
    txt.text = [[(MyBigObject *)[theDict objectForKey:key] box1] name];
    [self.view addSubview:txt];
}

在这种情况下,我需要知道用户何时更改任何文本 txt 并通过树更新相应的值。

In this case, I need to know when the user changes the text in any of the txt and update the appropriate value through the tree.

编辑2:尝试实施@Till的建议在评论中,我开始阅读 C语言指南教程。这似乎是我需要的方式,但我很难得到& * 来自C世界与我在Objective-C世界的最后一次编辑中提到的NSObject子类很好地配合。为了让@Till的建议起作用,我想我需要以某种方式将我的值的内存位置存储在复杂的对象树中。所以我上次编辑的代码块将成为:

EDIT 2: While trying to implement @Till's suggestion in the comments, I started reading a tutorial on pointers in C. It seems like this is the way I need to go, but I'm struggling to get the & and * from the C world to play nicely with the NSObject subclasses I mention in my last EDIT from the Objective-C world. To get @Till's suggestion to work, I think I need to somehow store the memory location of my value up that complicated object tree. So my code block from the last EDIT would become:

.h
@property (nonatomic, strong) NSMutableDictionary *tagDict;

.m
for (NSString *key in [theDict allKeys])
{
    UITextField *txt = [[UITextField alloc] initWithFrame:...];
    txt.text = [[(MyBigObject *)[theDict objectForKey:key] box1] name];
    txt.tag = globalCounter;
    [tagDict addObject:[[(MyBigObject *)[theDict objectForKey:key] box1] name] forKey:[NSString stringWithFormat:@"%d",globalCounter]];
    globalCounter ++;
    [self.view addSubview:txt];
}

其中 globalCounter 是一个 int 我每次把东西放进 tagDict 时都会递增,所以我可以让它保持唯一。我想在这种情况下,我可以简单地使用 NSArray ,它也可以正常工作,并且可能看起来更清洁一点。我的计划是在我的 testFieldDidEndEditing 中使用 tagDict ,如:

Where globalCounter is an int that I'm incrementing every time I put something into tagDict so I can keep it unique. I guess in this case I could just simply be using an NSArray and it would work just as well, and probably look a little cleaner. My plan is to use tagDict in my testFieldDidEndEditing like:

- (void) textFieldDidEndEditing:(UITextField *)textField
{
    *[tagDict objectForKey:[NSString stringWithFormat:@"%d",textField.tag]] = textField.txt;
}

这会引发错误分配给'id'来自不兼容的类型'NSString *'。我不太确定这意味着什么。有没有办法可以使用解除引用运算符来更改我在字典中指向的项目的值?

This throws an error for Assigning to 'id' from incompatible type 'NSString *'. I'm not really sure what that means. Is there a way I can use a "dereferencing operator" to change the value of the item I am pointing to in my dictionary?

抛出错误地址表达式必须是左值或函数指示符。关于这个指针的东西在Objective C世界中是如何工作的,我仍然有点模糊,我认为有些东西我还没有完全理解。

throws an error Address expression must be an lvalue or a function designator. I'm still a little fuzzy on how this pointer stuff would work in the Objective C world, and I think there's something I'm not fully understanding.

推荐答案

我最终做了很多我认为@Till试图提出的建议。我添加了一个 NSMutableArray * tieArray 和一个 int tieCounter 作为 @properties 到我的视图控制器。我的OP中第一个代码块构建 UITextFields 的代码现在是:

I ended up doing largely what I think @Till was trying to suggest. I added an NSMutableArray *tieArray and an int tieCounter as @properties to my view controller. My code for building the UITextFields from the first code block in my OP is now:

for (NSString *key in [theDict allKeys])
{
    UITextField *txt = [[UITextField alloc] initWithFrame:...];
    txt.text = [[(MyBigObject *)[theDict objectForKey:key] box1] name];
    [self.view addSubview:txt];

    txt.tag = self.tieCounter;
    self.tieCounter ++;
    [self.tieArray addObject:[[(MyBigObject *)[theDict objectForKey:key] box1] name]];
}

然后在我的 textFieldDidEndEditing

- (void) textFieldDidEndEditing:(UITextField *)textField
{
    if ([tieArray objectAtIndex:textField.tag] isKindOfClass:[NSMutableString class]])
    {
        [(NSMutableString *)[tieArray objectAtIndex:textField.tag] setString:textField.text];
    }
    else if (//...More conditions to set other variable types with the syntax as needed)
}

请注意,为此, MySmallObject1 MySmallObject2 需要由 isKindOfClass:检查覆盖,实际语法会有所不同,具体取决于该属性的类。

Note that for this to work, all the properties of MySmallObject1 and MySmallObject2 need to be covered by a isKindOfClass: check, and the actual syntax will vary a little depending on what class that property is.

我还没有能够测试这个(并且我的应用程序很可能在很长一段时间内都不会处于可测试状态),但这对我来说很有意义,而且它不会丢失任何错误或警告。如果我在实际运行时遇到问题,我会在这里更新。

I haven't been able to test this yet (and my app probably won't be in a testable state for quite some time), but it makes sense to me, and it's not throwing any errors or warnings. If I have problems with it when I actually run it I'll update here.

这篇关于有没有办法自动将UITextField绑定到我的数据模型中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆