这个 UITableView 代码有什么问题,我使用引用传递来更新核心数据配置记录?
我的 iPhone 应用程序中有核心数据配置,目前我如何将配置传递到允许用户编辑数据的视图,是通过引用传递核心数据对象。事实上,我将核心数据类属性(NSString)分配给编辑视图中的“UITextField.text”,这样,如果它被更新,那么它就会有效地更新核心数据对象。问题是:
- 当我在编辑视图中创建/更新此字符串时
- ,当我更新单独的字段(不是有问题的字段)时,该值似乎为空
问题 - 是否有任何根本错误使用核心数据托管对象属性通过引用传递方法?
代码片段:
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:
- it seems to work fine when I create/update this string in the edit view
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将托管对象的 NSString 属性分配给文本字段并编辑文本字段的内容将不会将这些更改传播回托管对象,原因有很多:
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:
似乎通过引用传递动态核心数据托管属性可能不起作用......?
所以这个答案是(用于投票/确认)通过引用传递动态核心数据托管属性是行不通的。
[我已更改为通过使用“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]
Greg,
有问题的行是:
并且
weConfig
是您的托管对象吗?并且nonWorkTerm
是一个NSString
?如果以上所有问题的答案都是肯定的,那么这是一个受支持的作业。 (我不知道为什么你认为这是按引用传递。看起来该值是直接分配给我的。[实际上,
titleTextField
很可能会复制该字符串。])什么是问题?
安德鲁
Greg,
The line in question is:
and
weConfig
is your managed object? andnonWorkTerm
is anNSString
?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