iPhone Note #17: Displaying a custom view controller from a UITextField
Problem: I want my textFieldSearchAddress to display a seperate viewcontroller.
Short Answer: Hide the keyboard by using
[textField resignFirstResponder]
then show the view controller.
- Implement a UITextFieldDelegate.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ [textField resignFirstResponder]; [self show]; return NO; } |
Be careful with the BOOL return of textFieldShouldBeginEditing. From the docs: “YES if an editing session should be initiated; otherwise, NO to disallow editing.”
- Show the view controller.
- (IBAction)show{ NSLog(@"show"); AddressViewController *addressViewController = [[AddressViewController alloc] initWithNibName:@"AddressViewController" bundle:nil]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:addressViewController]; [self presentModalViewController:nav animated:YES]; [addressViewController release]; [nav release]; } |