I've got a simple UIViewController with a UINavigationController. All that the view controller contains is a single UITextField. This view has two purposes:
- Create a new item. In this case, there are "Cancel" and "Save" buttons in the UINavigationController.
- Edit the name of an existing item. In this case, there's just the "Back" button in the top-left corner.
What I'd like is for the Return key on the iPhone keyboard to dismiss the UITextField.
Here's my textFieldShouldReturn code:
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if(self.navigationItem.rightBarButtonItem) {
//If we're creating a new item (there'd be a Save button in the top right)
[self saveItem]; //This method just saves the Core Data for this item.
[self.delegate addItemViewController:self didAddItem:item]; //This works fine; this method just tells the delegate to dismiss this view controller.
}else {
//If there's no button in the top-right corner, then we're editing an existing fridge.
[self saveItem]; //This method just saves the Core Data for this item.
[self.navigationController popViewControllerAnimated:YES]; //This is what doesn't work.
}
[textField resignFirstResponder];
return YES;
}
UITextField's delegate to your file owner? Also, are you sure yourUINavigationControlleris hooked up correctly? You may also want to try calling[textField resignFirstResponder];before the if, and putting anNSTimeron yourpopViewControllerAnimatedcall, just to make sure theUITextFieldhas enough time to resign. - msgambel