0

I have created a custom cell, containing a text field. I would like the keyboard to disappear when the user presses the done button (as seen in the screen shot below).

The custom cell is located in "AccountViewCell". In my code, I call and display this custom cell:

- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        static NSString *CellIdentifier = @"AccessCard";
        static NSString *Cellnib = @"AccountViewCell";

        AccountViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:Cellnib owner:self options:nil];
            cell = (AccountViewCell *)[nib objectAtIndex:3];
        }

        cell.data.text = [tableData objectAtIndex:indexPath.row];

        return cell;
    }

    if (indexPath.section == 1)
    {
        static NSString *CellIdentifier = @"Password";
        static NSString *Cellnib = @"AccountViewCell";

        AccountViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:Cellnib owner:self options:nil];
            cell = (AccountViewCell *)[nib objectAtIndex:4];
        }

        cell.data.text = [tableData objectAtIndex:indexPath.row];

        return cell;
    }

    return 0;
}

The user is able to input text, however I cannot seem to make the keyboard disappear.

I have also created a method in AccountViewCell to hide the keyboard:

- (void)textfieldInput
{
    UIToolbar* padToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    padToolBar.barStyle = UIBarStyleBlackTranslucent;

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Done"
                                   style:UIBarButtonItemStyleDone
                                   target:self
                                   action:@selector(doneWithPad)];
    [doneButton setWidth:65.0f];

    padToolBar.items = [NSArray arrayWithObjects:
                        [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelPad)],
                        [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                        doneButton,
                        nil];

    [padToolBar sizeToFit];
    textField.inputAccessoryView = padToolBar;

}

But when I call it in the cellForRowAtIndexPath, it does not work.

AccountViewCell* keyboard = [[AccountViewCell alloc] init];
[keyboard textfieldInput];

I am wondering if there is a way to hide the keyboard when the done key is pressed. A screen shot of my application is below:

keyboard does not disappear

Sulaiman Majeed
  • 501
  • 2
  • 7
  • 21
  • refer to this question: http://stackoverflow.com/questions/6906246/how-do-i-dismiss-the-ios-keyboard – Obj-Swift Feb 25 '13 at 16:47
  • I have tried that code as well, but it does not work. I am not quite sure as to where I should put it. [myTextField resignFirstResponder] Would I put it into the cellForRowAtIndexPath method or in the AccountViewCell class? – Sulaiman Majeed Feb 25 '13 at 16:50
  • - (IBAction)dismissKeyboard:(id)sender { [aTextBox resignFirstResponder]; } – Obj-Swift Feb 25 '13 at 16:55
  • http://stackoverflow.com/questions/274319/how-do-you-dismiss-the-keyboard-when-editing-a-uitextfield – Obj-Swift Feb 25 '13 at 16:57
  • @SulaimanMajeed See the second link provided by akash. That other question has answers to your question. – rmaddy Feb 25 '13 at 17:49

4 Answers4

1

In the Done button's action method use this one line code:

[myTextField resignFirstResponder];

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • What do you mean by "Make my control's delegate to my class"? How would I go about doing this? And then, would I put that one line of code into my CellForRowAtIndexPath method or in my AccountViewCell class? – Sulaiman Majeed Feb 25 '13 at 16:54
  • On your done button's action method put this line of code. – Anoop Vaidya Feb 25 '13 at 16:55
1

Inlucde UITextFeildDelegate method in #.h file

Provide [textfeild setDelegate:self];

[textField setReturnKeyType:UIReturnKeyDone];

include

- (BOOL)textFieldShouldReturn:(UITextField *)textField
 {
[textField resignFirstResponder];
 }
Madhu
  • 1,542
  • 1
  • 14
  • 30
  • The `textFieldDidEndEditing:` delegate method is called when the text field is no longer the first responder. So it makes no sense to call `resignFirstResponder` in this method. And none of this will be called when the "return" key is pressed. – rmaddy Feb 25 '13 at 17:47
  • check with my answer. Its unfortunately displayed that method. – Madhu Feb 26 '13 at 05:21
0

You must impliment the UITextViewDelegate in your header file, and then set the delegate of your text views to be your delegate. Often you'd do this all in the same class file so you could do

aTextField.delegate = self;

Once your delegate is set you can use the appropriate delegate method:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textView resignFirstResponder];
} 
WhoaItsAFactorial
  • 3,538
  • 4
  • 28
  • 45
  • This is not the proper delegate method for handling the "return" key being pressed in a text field. – rmaddy Feb 25 '13 at 17:48
0

When connecting your UITextField in the .xib file, connect the destination to DidEndOnExit.