Created
June 11, 2011 16:26
-
-
Save binho/1020724 to your computer and use it in GitHub Desktop.
best way to toggle done button on UITextField and UITextView (iPhone)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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