I'm trying to dismiss the keyboard when the user presses a "cancel" UIBarButtonItem. When I click the cancel button however, I get a SIGABRT with the "unrecognized selector sent to instance" error.
My code to create the cancel button is:
- (void)keyboardWasShown:(NSNotification*)aNotification
{
//Add cancel button to navigation bar
UIBarButtonItem *dismissKeyboardBttn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissKeyboard:)];
self.navigationItem.rightBarButtonItem = dismissKeyboardBttn;
}
And to dismiss the keyboard I have this method:
- (void)dismissKeyboard:(id)sender
{
[activeField resignFirstResponder];
//^^This line causes the SIGABRT^^
}
It seems pretty straightforward. Any ideas?
UPDATE: activeField is just a UITextField I'm using to move my scrollView to the UITextField the user is currently editing. It is set in these two methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
UPDATE 2: Interesting, I 开发者_如何学Chave registered my ViewController to receive keyboard notifications, and when I try to dismiss the keyboard using the "textFieldShouldReturn" method, I get the same error. Here is my textFieldShouldReturn code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField canResignFirstResponder])
{
[textField resignFirstResponder];
}
return YES;
}
I am in a situation and do as follows in the current view controller:
In the header file, create an IBAction for the text field which became the first responder and bring up the keyboard:
- (IBAction)textFieldDidBeginEditing:(UITextField *)textField;
In the implementation file, create a method which creates the bar button (in my case, a "Done" button) and adds it to the right side of the navigation bar. Simultaneously, I create a target action pairing between the TextField (which has become the first responder
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// create new bar button with "Done" as text
// set the target of the action as the text field (since we want the text field to resign first responder status and dismiss the keyboard)
// tell the text field to resign with the stock 'resignFirstResponder' selector
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:textField
action:@selector(resignFirstResponder)];
// add the button with target/action pairing to the navigation bar
[[self navigationItem] setRightBarButtonItem:bbi];
}
Additionally, if you want the button to disappear after I click it (and the keyboard goes away), I use the textFieldDidEndEditing since editing has now completed with the first responder recognition:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[[self navigationItem] setRightBarButtonItem:nil];
}
What is activeField? If it's a UIResponder, it should respond to resignFirstResponder. So maybe it's not. UIViews and UIViewControllers are UIResponders.
Morningstar is right, what is the activeField, is it an id and you possible need to add a cast: (UIButton*)
? Also, I always add this when resignFirstResponder
:
if(myObject canResignFirstResponder){
}
精彩评论