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