按下完成后键盘不会消失

时间:2014-04-08 15:57:00

标签: ios keyboard

我有以下代码,当我在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;
}

头文件委托:

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

这是我在表格中为cellForRowAtIndexPath方法创建字段的地方:

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];
        }

1 个答案:

答案 0 :(得分:1)

问题是您处于UIModalPresentationFormSheet模式视图控制器的视图中。默认情况下,当此类模式视图上的文本字段取消第一响应者时,键盘消失。仅当模态视图本身被解除时键盘才会消失(或者您可以通过覆盖disablesAutomaticKeyboardDismissal来返回NO来改变行为。)