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

4

4 回答 4

1

# .hUITextFeildDelegate文件中包含方法

提供[textfeild setDelegate:self];

[textField setReturnKeyType:UIReturnKeyDone];

包括

- (BOOL)textFieldShouldReturn:(UITextField *)textField
 {
[textField resignFirstResponder];
 }
于 2013-02-25T16:56:28.570 回答
1

在完成按钮的操作方法中使用这一行代码:

[myTextField resignFirstResponder];

于 2013-02-25T16:49:11.670 回答
0

您必须UITextViewDelegate在头文件中隐含 ,然后将文本视图的委托设置为您的委托。通常你会在同一个类文件中做这一切,所以你可以做

aTextField.delegate = self;

设置好委托后,您可以使用适当的委托方法:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textView resignFirstResponder];
} 
于 2013-02-25T16:57:07.173 回答
0

在 .xib 文件中连接 UITextField 时,将目标连接到 DidEndOnExit。

于 2013-02-26T01:04:30.397 回答