In the Cell.m i have written this code to add some strings to a dictionary:
- (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];
}
And then, in the TableViewController.m:
-(IBAction)doneButtonPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
The user is now back to the DetailViewController with the info about the object plus the new info added by the user. The info is extracted from the plist in viewWillAppear.
But the problem is that if the user hits the Done button without dismissing the keyboard first (triggering textViewDidEndEditing), the text from the last edited cell is not displayed in the DetailViewController.
But if the user goes back one more step and then re-enter the object (DetailViewController), the text is there. So textViewDidEndEditing is triggered too late for viewWillAppear in the parent view to grab the text when Done button is hit.
How to solve this?
In the method that handles the ‘Done’ button, call
[self.view endEditing:YES]as the first line of the method. This will cause the keyboard to be dismissed allowing you to process the data before the screen is dismissed.