gpt4 book ai didi

ios - UITextField 不会将文本保存到 CoreData 中

转载 作者:行者123 更新时间:2023-11-29 04:04:51 25 4
gpt4 key购买 nike

此 UITextField 以编程方式创建并驻留在自定义 UITableViewCell 中。 UITableViewCell 也是 UITextField 的委托(delegate)并实现 <UITextFieldDelegate>协议(protocol)。一切似乎都工作正常,除了当我重新加载应用程序时,没有任何文本保存在 CoreData 中。我尝试使 TableViewController 成为此类的委托(delegate),但它似乎不起作用,实际上,由于发送到实例错误的无法识别的选择器,应用程序崩溃了。

这是我的 textFieldDidEndEditing 的代码方法:

- (void)textFieldDidEndEditing:(UITextField *)textField {

Parts *item ; // the NSManagedObject

item.itemTitle = _itemLabel.text; //_itemLabel is a custom UITextField Class, and itemTitle is a string attribute of the NSManagedObject


NSError *error;
[item.managedObjectContext save:&error];
if (error) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

}

这是自定义的部分init创建 UITextField 的方法:

_itemLabel = [[TehdaLabel alloc] initWithFrame:CGRectNull];
_itemLabel.textColor = [UIColor blackColor];
_itemLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
_itemLabel.backgroundColor = [UIColor clearColor];
_itemLabel.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_itemLabel.returnKeyType = UIReturnKeyDone;
_itemLabel.placeholder = @"Type some stuff here!";
_itemLabel.delegate = self;
[self addSubview:_itemLabel];

其他一切都工作正常,将文本保存在 CoreData 中只是拒绝为我工作。

编辑:insertObject我的 TableViewController 中的方法

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
Parts *item = [NSEntityDescription insertNewObjectForEntityForName:@"Parts" inManagedObjectContext:context];

MCSwipeTableViewCell *editCell;
for (MCSwipeTableViewCell *cell in [self.tableView visibleCells]){
if (cell.itemLabel.text == item.itemTitle) {
//if (cell.itemLabel.text == item.itemTitle) {
editCell = cell;
break;
}
}
editCell.itemLabel.text = item.itemTitle;
[editCell.itemLabel becomeFirstResponder];

[item setValue:[NSDate date] forKey:@"itemDate"];

// Save the context.
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

最佳答案

您没有将实体“Parts”插入 CoreData。

Parts *item = [NSEntityDescription insertNewObjectForEntityForName:@"Parts"
inManagedObjectContext:managedObjectContext];

现在它将保存它......

关于ios - UITextField 不会将文本保存到 CoreData 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422457/

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