Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未及时更新用于保存TextFieldDiEndEdit的Textfield值_Ios_Core Data_Uitextfield - Fatal编程技术网

Ios 未及时更新用于保存TextFieldDiEndEdit的Textfield值

Ios 未及时更新用于保存TextFieldDiEndEdit的Textfield值,ios,core-data,uitextfield,Ios,Core Data,Uitextfield,我正在开发一个使用核心数据的简单应用程序。我有一个带有文本字段的详细屏幕,我想在单击“完成”按钮时保存该字段。现在,在调用textfielddidediting之前完成done操作 (使用xcode 6) 我可以将现场输入记录到控制台。因此,我认为我已经正确设置了出口,并建立了代理关系 以下是我认为相关的代码: - (void) textFieldDidEndEditing:(UITextField *)textField { self.seasonName = textField.te

我正在开发一个使用核心数据的简单应用程序。我有一个带有文本字段的详细屏幕,我想在单击“完成”按钮时保存该字段。现在,在调用textfielddidediting之前完成done操作

(使用xcode 6)

我可以将现场输入记录到控制台。因此,我认为我已经正确设置了出口,并建立了代理关系

以下是我认为相关的代码:

- (void) textFieldDidEndEditing:(UITextField *)textField
{
    self.seasonName = textField.text;
    NSLog(@"Did End Editing: %@", self.seasonName);
}

- (IBAction)done:(UIBarButtonItem *)sender {

    Season *season = nil;

    if (self.seasonToEdit) {
        season = self.seasonToEdit;
    } else {
        season = [NSEntityDescription insertNewObjectForEntityForName:@"Season" inManagedObjectContext:self.managedObjectContext];
    }

    season.seasonName = self.seasonName;
    //season.seasonDescription = self.seasonDescriptionTextView.text;

    NSLog(@"Season name: %@", season.seasonName);

    [self dismissViewControllerAnimated:YES completion:nil];
}
从这个日志中可以看到,我从TextFieldDiEndediting调用之前的done操作中得到消息:

2014-10-08 16:36:10.318 ScorerCD[12277:475893] Season name: (null)
2014-10-08 16:36:10.842 ScorerCD[12277:475893] Did End Editing: the 
我的问题在哪里?谢谢

编辑:我尝试在“完成”方法的顶部辞职FirstResponder,但没有产生任何影响。此外,我还尝试设置season.seasonName=self.seasonNameTextField.text,但没有更改

另一种编辑:如果我使用此代码时,无论是否使用self.seasyname=newText,我都会看到文本字段中输入了字符,但self.seasyname textfield.text在我尝试检索它时仍然为空

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];

    self.doneBarButton.enabled = ([newText length] > 0);

    NSLog(@"In change characters");

    self.seasonName = newText;

    return YES;
}
编辑:更多日志信息

2014-10-09 09:46:33.280 ScorerCD[13179:517301] New Season
2014-10-09 09:46:37.788 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:37.792 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:42.104 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:42.343 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:42.627 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:45.112 ScorerCD[13179:517301] Season to edit: (null)
2014-10-09 09:46:45.194 ScorerCD[13179:517301] Season: <Season: 0x7be3b7d0> (entity: Season; id: 0x7be323c0 <x-coredata:///Season/t7CBA137F-F348-47E6-A0EA-2518DAF422392> ; data: {
    games = nil;
    seasonDescription = nil;
    seasonName = nil;
})
2014-10-09 09:46:45.194 ScorerCD[13179:517301] Season name: (null)
2014-10-09 09:46:45.712 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:45.744 ScorerCD[13179:517301] Did End Editing: the high

单击新的UI控件后,文本字段的焦点会丢失。因此,在DidEndEdition启动之前,您将始终首先调用按钮功能

你可以用

textViewDidChange
要更新您的季节名称,请参见以下答案--

基本上,键盘关闭时会调用
textfielddidediting
。这可能要等到你点击“完成”按钮之后


因此,您可以在
done
方法中检查您的文本字段,或者使用
-(BOOL)textField应该返回:(UITextField*)textField

-(void)textfieldEndediting:(UITextField*))textField
在您的文本字段退出第一响应程序后被调用,这是在您单击“保存”按钮之后,因此没有问题。如果您有文本字段出口,为什么不直接检查该出口并赋值?

看到这一点,文本字段将作为您在“完成”方法结束时解除视图控制器的一部分而被放弃。以前没有。 如果您想更快地获取textfields文本,您可以将该字段链接为一个出口,并从中获取文本值,即self.mytextfield.text,或者在done方法顶部的文本字段上调用resignFirstResponder

编辑:

为textfield创建一个出口(并将其链接到interface builder中的textfield):

然后更新您的done方法

- (IBAction)done:(UIBarButtonItem *)sender {

    Season *season = nil;

    if (self.seasonToEdit != nil) {
        season = self.seasonToEdit;
    } else {
        season = [NSEntityDescription insertNewObjectForEntityForName:@"Season" inManagedObjectContext:self.managedObjectContext];
    }

    season.seasonName = self.seasonNameTextField.text;

    [self dismissViewControllerAnimated:YES completion:nil];
}

确保插座已连接到interface builder中。我在我的答案中添加了一些代码,这些代码应该有助于您的兴趣,请注销这些代码并查看它们返回的NSLog(@“要编辑的季节:%@”,self.seasuretoedit);NSLog(@“季节:%@”,季节)@Tom Bates他们都显示为空我编辑了我的问题以显示我得到的日志。你应该这样做。将if更改为if(self.sequencetoedit!=nil),以确保在加载视图时,如果我是您,我会选择季节,并在文本字段使用UITextFieldDelegates更改其文本时更新名称。空问题是因为它不是空的,而是零。我认为(self.seventiontoedit)问题是一个新的xcode 6问题。不过,我对课文不太清楚。可能会将出口参考更改为strong,但我怀疑这是解决方案谢谢。我有确切的代码,我的出口是连接的(当我在IBOutlet语句旁边的点上悬停时我可以看到),但我会得到一个空的self.seasurenametextfield.text我完全困惑,因为你的回答很有道理。谢谢。我尝试添加这两个选项,但仍然得到空的self.seasonNameTextField.text。就像文本firld值没有被传递一样。
@property (nonatomic, weak) IBOutlet UITextfield *seasonNameTextField;
- (IBAction)done:(UIBarButtonItem *)sender {

    Season *season = nil;

    if (self.seasonToEdit != nil) {
        season = self.seasonToEdit;
    } else {
        season = [NSEntityDescription insertNewObjectForEntityForName:@"Season" inManagedObjectContext:self.managedObjectContext];
    }

    season.seasonName = self.seasonNameTextField.text;

    [self dismissViewControllerAnimated:YES completion:nil];
}