0

在 Cell.mi 中编写了这段代码来将一些字符串添加到字典中:

- (void)textViewDidEndEditing:(UITextView *)textView {

if (cellIndex == 0) {
[[dataSource objectAtIndex:dataIndex] setObject:self.cellTextView.text forKey:@"Usersmell"];
}
if (cellIndex == 1) {
    [[dataSource objectAtIndex:dataIndex] setObject:self.cellTextView.text forKey:@"Usertaste"];
}
if (cellIndex == 2) {
    [[dataSource objectAtIndex:dataIndex] setObject:self.cellTextView.text forKey:@"Usersuits"];
}
if (cellIndex == 3) {
    [[dataSource objectAtIndex:dataIndex] setObject:self.cellTextView.text forKey:@"Usernotes"];
}

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Objects.plist"];

//removed some code here that gets the index of dictionary for replacement, then:

[allObjectsArray replaceObjectAtIndex:index withObject:[NSDictionary dictionaryWithDictionary:[dataSource objectAtIndex:dataIndex]]];
[allObjectsArray writeToFile:path atomically:YES];
}

然后,在 TableViewController.m 中:

-(IBAction)doneButtonPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}

用户现在返回到 DetailViewController,其中包含有关对象的信息以及用户添加的新信息。该信息是从 viewWillAppear 中的 plist 中提取的。

但问题是,如果用户在没有先关闭键盘的情况下点击“完成”按钮(触发 textViewDidEndEditing),则最后编辑的单元格中的文本不会显示在 DetailViewController 中。

但是如果用户再退一步然后重新输入对象(DetailViewController),文本就在那里。所以 textViewDidEndEditing 被触发太晚,以至于父视图中的 viewWillAppear 在点击完成按钮时抓取文本。

如何解决这个问题?

4

1 回答 1

2

在处理“完成”按钮的方法中,[self.view endEditing:YES]作为方法的第一行调用。这将导致键盘关闭,允许您在屏幕关闭之前处理数据。

-(IBAction)doneButtonPressed:(id)sender {
    [self.view endEditing:YES];

    [self.navigationController popViewControllerAnimated:YES];
}
于 2012-10-29T16:28:24.877 回答