I have the following code and my keyboard is not dismissing when the Done button is pressed on the iPad.

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    if (textField.tag == 13) //Username Field
    {
        UITableViewCell *cCredentials = [self.tvList cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:CredentialsSection]];
        [((UITextField*)[cCredentials viewWithTag:14]) becomeFirstResponder];
    }
    else if (textField.tag == 14) //Password Field
    {
        if (![[[NSUserDefaults standardUserDefaults] stringForKey:@"NTAuthentication"] isEqualToString: @"N"])
        {
            //Domain Field
            UITableViewCell *cCredentials = [self.tvList cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:CredentialsSection]];
            [((UITextField*)[cCredentials viewWithTag:15]) becomeFirstResponder];
        }
        else
        {
            [textField resignFirstResponder];
        }
    }
    else
    {
        [textField resignFirstResponder];
    }

    return YES;
}

The header file delegates:

@interface vcSignature_iPad : UIViewController <DropdownDelegate,
                                                UITextFieldDelegate,
                                                UITextViewDelegate,
                                                UITableViewDataSource,
                                                UITableViewDelegate>

This is where I create the fields in the table for the cellForRowAtIndexPath method:

UITableViewCell *cellCredentials = [tableView dequeueReusableCellWithIdentifier:@"cellCredentials"];

    if (cellCredentials == nil)
    {
        cellCredentials = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellCredentials"];
        cellCredentials.selectionStyle = UITableViewCellSelectionStyleNone;
        cellCredentials.backgroundColor = [UIColor clearColor];

        UITextField *txt = [[UITextField alloc]init];
        txt.borderStyle = UITextBorderStyleRoundedRect;
        txt.font = [UIFont systemFontOfSize:14.0];
        txt.textAlignment = NSTextAlignmentLeft;
        txt.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        txt.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        txt.delegate = self;
        txt.text = nil;

        if (indexPath.row == 0)
        {
            txt.placeholder = @"User Id";
            txt.tag = 13;
            txt.returnKeyType = UIReturnKeyNext;
            txt.frame = CGRectMake(20.0, 10.0, 240.0, 31.0);
            txt.text = stored.User;
            [cellCredentials.contentView addSubview:txt];


            txt = [[UITextField alloc]init];
            txt.borderStyle = UITextBorderStyleRoundedRect;
            txt.font = [UIFont systemFontOfSize:14.0];
            txt.textAlignment = NSTextAlignmentLeft;
            txt.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            txt.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            txt.delegate = self;
            txt.text = stored.Password;

            txt.placeholder = @"Password";
            txt.secureTextEntry = YES;
            txt.tag = 14;
            txt.frame = CGRectMake(279.0, 10.0, 240.0, 31.0);
            if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"NTAuthentication"] isEqualToString: @"N"])
            {
                txt.returnKeyType = UIReturnKeyDone;
            }
            else
            {
                txt.returnKeyType = UIReturnKeyNext;
            }
            [cellCredentials.contentView addSubview:txt];
        }
        else
        {
            txt.placeholder = @"Domain";
            txt.tag = 15;
            txt.returnKeyType = UIReturnKeyDone;
            txt.frame = CGRectMake(20.0, 10.0, 240.0, 31.0);
            txt.text = stored.Domain;
            [cellCredentials.contentView addSubview:txt];
        }
有帮助吗?

解决方案

The problem is that you are in a UIModalPresentationFormSheet modal view controller's view. By default, the keyboard does not disappear when a text field on this kind of modal view resigns first responder. The keyboard vanishes only when the modal view itself is dismissed (or you can change the behavior, by overriding disablesAutomaticKeyboardDismissal to return NO).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top