这个 UITableView 代码有什么问题,我使用引用传递来更新核心数据配置记录?

发布于 2024-12-08 12:56:39 字数 2355 浏览 2 评论 0原文

我的 iPhone 应用程序中有核心数据配置,目前我如何将配置传递到允许用户编辑数据的视图,是通过引用传递核心数据对象。事实上,我将核心数据类属性(NSString)分配给编辑视图中的“UITextField.text”,这样,如果它被更新,那么它就会有效地更新核心数据对象。问题是:

  1. 当我在编辑视图中创建/更新此字符串时
  2. ,当我更新单独的字段(不是有问题的字段)时,该值似乎为空

问题 - 是否有任何根本错误使用核心数据托管对象属性通过引用传递方法?

代码片段:

a) 在编辑控制器中 - 在 cellForRowAtIndexPath

if (indexPath.row == 0) {
        // Setup
    UITextField *titleTextField = nil;
    // Get Cell
    self.nonWorkTermCell = [tableView dequeueReusableCellWithIdentifier:@"nonWorkTermCell"];
    if (self.nonWorkTermCell == nil) {
        self.nonWorkTermCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"nonWorkTermCell"] autorelease];
            self.nonWorkTermCell.textLabel.text = @"Non Work Term:";

            titleTextField = [[self newTextFieldForCell:self.nonWorkTermCell] autorelease];
            titleTextField.keyboardType = UIKeyboardTypeURL;
            titleTextField.returnKeyType = UIReturnKeyDone; 
        titleTextField.delegate = self;  // TODO: Is this needed at all?
        titleTextField.tag = 1;
            [self.nonWorkTermCell addSubview:titleTextField];
    }
    // Set Value
    titleTextField.text = self.weConfig.nonWorkTerm;   // ** ASSIGNS THE CORE DATA MANAGED OBJECT CONFIG ITEM DIRECTLY TO THE TEXT FIELD

        // Return 
    return self.nonWorkTermCell;

b) 及其使用的辅助方法:

- (UITextField *)newTextFieldForCell:(UITableViewCell *)cell {
    UITextField *addTextField = [[UITextField alloc] initWithFrame:frame];  //cut the code for the frame to save space
    addTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    addTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    addTextField.delegate = self;
    addTextField.clearButtonMode = UITextFieldViewModeNever;
    addTextField.enabled = YES;
    addTextField.returnKeyType = UIReturnKeyDone;
    addTextField.clearButtonMode = UITextFieldViewModeWhileEditing;  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return addTextField;
}

c) 然后回到委托中

- (void)applicationWillResignActive:(UIApplication *)application {
    [self saveContext];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [self saveContext];
} 

I have core data configuration in my iPhone app, and how I'm currently passing the configuration to the view that allows a user to EDIT the data, is via passing the core data object by reference. In fact I'm allocated the core data class attribute (NSString) to the "UITextField.text" in the edit view, so that if it gets updated then it is effectively updating the core data object. The issue is:

  1. it seems to work fine when I create/update this string in the edit view
  2. when I update a separate field (not the one in question) then the value seems to be null

Question - Is there anything fundamentally wrong with this pass-by-reference approach using a core-data managed object attribute?

Code snippets:

a) In the Edit Controller - in cellForRowAtIndexPath

if (indexPath.row == 0) {
        // Setup
    UITextField *titleTextField = nil;
    // Get Cell
    self.nonWorkTermCell = [tableView dequeueReusableCellWithIdentifier:@"nonWorkTermCell"];
    if (self.nonWorkTermCell == nil) {
        self.nonWorkTermCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"nonWorkTermCell"] autorelease];
            self.nonWorkTermCell.textLabel.text = @"Non Work Term:";

            titleTextField = [[self newTextFieldForCell:self.nonWorkTermCell] autorelease];
            titleTextField.keyboardType = UIKeyboardTypeURL;
            titleTextField.returnKeyType = UIReturnKeyDone; 
        titleTextField.delegate = self;  // TODO: Is this needed at all?
        titleTextField.tag = 1;
            [self.nonWorkTermCell addSubview:titleTextField];
    }
    // Set Value
    titleTextField.text = self.weConfig.nonWorkTerm;   // ** ASSIGNS THE CORE DATA MANAGED OBJECT CONFIG ITEM DIRECTLY TO THE TEXT FIELD

        // Return 
    return self.nonWorkTermCell;

b) and the helper method it uses:

- (UITextField *)newTextFieldForCell:(UITableViewCell *)cell {
    UITextField *addTextField = [[UITextField alloc] initWithFrame:frame];  //cut the code for the frame to save space
    addTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    addTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    addTextField.delegate = self;
    addTextField.clearButtonMode = UITextFieldViewModeNever;
    addTextField.enabled = YES;
    addTextField.returnKeyType = UIReturnKeyDone;
    addTextField.clearButtonMode = UITextFieldViewModeWhileEditing;  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return addTextField;
}

c) then back in the delegate

- (void)applicationWillResignActive:(UIApplication *)application {
    [self saveContext];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [self saveContext];
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬心 2024-12-15 12:56:39

将托管对象的 NSString 属性分配给文本字段并编辑文本字段的内容将不会将这些更改传播回托管对象,原因有很多:

  • NSString 是不可变的,因此根据定义,您不会将 这些更改传播回托管对象对同一个对象进行更改
  • 托管对象上的访问器返回的对象很可能是基础值的自动释放副本
  • 文本字段也可能采用分配给其文本属性的任何值的副本
  • 任何更改都不会返回到模型除非他们已经通过了包含 KVO 通知的托管对象访问器,
  • 整个想法很疯狂。如果我想获取从托管对象获得的字符串并对其执行一些其他操作以进行演示或分析,该怎么办?

Assigning a managed object's NSString property to a textField and the editing the textfield's content will not propagate those changes back to the managed object, for a number of reasons:

  • NSString is immutable, so by definition you won't be making changes to the same object
  • The object returned by an accessor on managed object could well be an autoreleased copy of the underlying value
  • The textfield also probably takes a copy of whatever value is assigned to it's text property
  • No changes will go back to the model unless they have passed through the managed object accessors which include the KVO notifications
  • The whole idea is insane. What if I wanted to take a string that I'd got from a managed object and perform some other operation on it for presentation or analysis?
仲春光 2024-12-15 12:56:39

似乎通过引用传递动态核心数据托管属性可能不起作用......?

所以这个答案是(用于投票/确认)通过引用传递动态核心数据托管属性是行不通的。

[我已更改为通过使用“textFieldDidEndEditing”手动设置模型,这似乎解决了问题]

It seems like passing a dynamic core data managed property by reference may not work....?

So this answer is (for voting/confirmation) that passing a dynamic core data managed property by reference doesn't work.

[I've changed to manually setting the model via use of "textFieldDidEndEditing" which seems to fix things]

旧梦荧光笔 2024-12-15 12:56:39

Greg,

有问题的行是:

titleTextField.text = self.weConfig.nonWorkTerm;

并且 weConfig 是您的托管对象吗?并且 nonWorkTerm 是一个 NSString

如果以上所有问题的答案都是肯定的,那么这是一个受支持的作业。 (我不知道为什么你认为这是按引用传递。看起来该值是直接分配给我的。[实际上,titleTextField很可能会复制该字符串。])

什么是问题?

安德鲁

Greg,

The line in question is:

titleTextField.text = self.weConfig.nonWorkTerm;

and weConfig is your managed object? and nonWorkTerm is an NSString?

If the answer to everything above is yes, then this is a supported assignment. (I'm not sure why you think this is a pass by reference. It looks like the value is being directly assigned to me. [Actually, titleTextField will most likely copy the string.])

What is the problem?

Andrew

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文