gpt4 book ai didi

ios - 如何将字段从 NSManagedObject 传递到 Controller 以对其进行修改

转载 作者:行者123 更新时间:2023-11-29 02:01:47 26 4
gpt4 key购买 nike

我有一个 NSManaged 对象,其中包含多个字符串字段,例如:

@interface myObject : NSManagedObject

@property (nonatomic, copy) NSString * name0;
@property (nonatomic, copy) NSString * name1;
@property (nonatomic, copy) NSString * name2;

我有一个 TableView Controller ,它显示该对象的属性,当有人单击某个单元格时,它会调用第二个 View Controller (下面的 EditTableViewController),该 Controller 将允许修改该特定单元格。我想将它需要修改的 NSString* 传递给第二个 Controller (取决于单击的单元格)。

因此我为第二个 Controller 尝试了类似的操作:

@interface EditTableViewController : UITableViewController
@property (nonatomic) NSString *value;

在实现部分我有(除其他外):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

EditPropertyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditPropertyCell"];
cell.valueTextField.text = self.value;
cell.valueTextField.delegate = self;
[cell.valueTextField becomeFirstResponder];
[cell.valueTextField setReturnKeyType:UIReturnKeyDone];
return cell;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
self.value = textField.text;
[self.navigationController popViewControllerAnimated:TRUE];
return YES;
}

第一个 Controller 拥有对 myObject 类型的对象的引用

@interface myObjectDetailTableViewController : UITableViewController
@property (nonatomic, strong) myObject * myObject;

并通过以下方式调用第二个 Controller :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

EditTableViewController *etvc = [[EditTableViewController alloc] initWithStyle:UITableViewStyleGrouped];

if ([indexPath row] ==0) {
etvc.value = self.myObject.name0;
}
if ([indexPath row] ==1) {
etvc.value = self.myObject.name1;
}
if ([indexPath row] ==2) {
etvc.value = self.myObject.name2;
}
[self.navigationController pushViewController:etvc
animated:YES];

问题是它不起作用,即当我在 EditTableViewController 中时,我可以正确地看到我想要修改的值(例如 name0),但如果我修改它,则不会保存它。我猜 NSString* 已被复制到某个地方,但我不知道在哪里。我想知道它是否与 NSManagedObject 有关?

无论如何,也许我尝试执行此操作的方式完全不合适,在这种情况下,我也很高兴知道最好的方法是什么

(特别是,稍后我希望理想情况下也能够设置以这种方式设置为 nil 的字段)。

最佳答案

您不能只传递字符串,因为您需要告诉托管对象字符串正在更改,您可以通过调用该属性的托管对象 setter 方法来做到这一点。

一个更好的选择是同时传递托管对象和作为应该更改的键的字符串。然后,您的 Controller 可以使用该键获取要显示的现有值,并设置该键的值以更新托管对象(然后需要保存该对象以存储更新)。

关于ios - 如何将字段从 NSManagedObject 传递到 Controller 以对其进行修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30276995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com