【问题标题】:Modify CoreData entity attribute value via UITextView通过 UITextView 修改 CoreData 实体属性值
【发布时间】:2013-05-23 10:20:15
【问题描述】:
我有 UITextView,它显示了来自 CoreData 实体的一些文本数据。
我需要在键盘关闭后编辑此文本数据并保存更改。
怎么做?
我知道如何保存数据,但我不知道如果我在UITextView 外面点击时如何关闭键盘。可能有一些本地方法可以做到这一点。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if ([appDelegate.managedObjectContext save:&error]) {
...
}
感谢您的帮助或建议!
【问题讨论】:
标签:
ios
core-data
uitextfield
appdelegate
【解决方案1】:
首先,您需要将 UITextview 的 delegate 设置为当前视图。我们有本地代码,当我们点击键盘上的返回按钮时,我们将使用 UIKit.Framework,在该函数内部编写你的保存或其他逻辑。
代码:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
// ------- here is your logics -------- //
}
textField => 你的文本字段对象。
【解决方案2】:
-
要在您停止编辑 UITextView 并关闭键盘后保存数据,
使用TextView的Delegate方法
- (void)textViewDidEndEditing:(UITextView *)textView
在此方法中,将新文本保存在 Entity 的相应属性中,并保存托管对象上下文以持久化数据。
-
在 UITextView 外部点击时关闭键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[
TextViewresignFirstResponder];
}
【解决方案3】:
要关闭您可以使用的键盘。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}