TechMan
TechMan

Reputation: 31

Hide keyboard/ resignFirstResponder forcibly

i'm working on an app which has a tableView with a textField in the right side of its each cell(there are more than 20 cells). i've created custom cell's for each row except for the last one. In the last row there is only a button.

Now i want to call resignFirstResponder on the button's click.
What should i do Please help?

Upvotes: 3

Views: 1723

Answers (4)

mAc
mAc

Reputation: 2514

I think this link will help you:

Objective C: ResignFirstResponder with a button

Provide some code so that, it will be easier to help u.

Hope this helps you. :)

Upvotes: 0

Thomas Nadin
Thomas Nadin

Reputation: 1207

Aopsfan's answer is probably the best solution so far. However, to add to it (as I cannot post comments), do remember to deallocate the object:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (currentTextField != nil) {
        [currentTextField release];
    }
    currentTextField = [textField retain];
}

Better still use @property's and @synthesize so the runtime can do the memory management for you.

[ViewController].h

@property (nonatomic, retain) UITextField* currentTextField;

[ViewController].m

@synthesize currentTextField = _currentTextField;

- (void)viewDidLoad|Appear {
    self.currentTextField = nil;
}

- (void) dealloc {
    [_currentTextField release], _currentTextField = nil;
    ...
    [super dealloc];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentTextField = textField;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.currentTextField) {
        [self.currentTextField resignFirstResponder];
    }
}

Upvotes: 1

aopsfan
aopsfan

Reputation: 2441

You probably want to keep track of the text field with the keyboard. Implement the <UITextFieldDelegate> protocol in your controller, and set the controller as each of the text fields' delegates. Write the textFieldDidBeginEditing: method like so, setting an instance variable called currentTextField:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    currentTextField = [textField retain];
}

Then, in your action for the button run [currentTextField resignFirstResponder].

Upvotes: 2

Totumus Maximus
Totumus Maximus

Reputation: 7573

You will have to keep track of which textfield in which cell has the first responder and resign it like this.

[myCellTextField resignFirstResponder];

Upvotes: 2

Related Questions