Skip to content

Instantly share code, notes, and snippets.

@binho
Created June 11, 2011 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binho/1020724 to your computer and use it in GitHub Desktop.
Save binho/1020724 to your computer and use it in GitHub Desktop.
best way to toggle done button on UITextField and UITextView (iPhone)
/*
set the UITextField's and UITextView's delegates to file owners!
put at .h file
*/
id activeField;
/*
put at .m file
*/
// -------------- Show/hide done button delegates --------------
- (void)textViewDidEndEditing:(UITextView *)textView
{
activeField = nil;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
activeField = textView;
NSLog(@"activeField: (UITextView) %@", activeField);
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(dismissKeyboard)] autorelease];
self.navigationItem.rightBarButtonItem = done;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
NSLog(@"activeField: (UITextField) %@", activeField);
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(dismissKeyboard)] autorelease];
self.navigationItem.rightBarButtonItem = done;
}
- (void)dismissKeyboard
{
NSLog(@"dismissKeyboard with active field: %@", activeField);
[activeField resignFirstResponder];
// remove done button from navigation
self.navigationItem.rightBarButtonItem = nil;
}
// -------------- Show/hide done button delegates --------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment