弹出viewcontroller iOS时键盘会短暂出现

时间:2015-08-19 12:37:37

标签: ios objective-c uiviewcontroller keyboard

当我从UINavigationController弹出当前的UIViewController时,我正在试验数字键盘的问题。

在我当前的UIViewController中。我有一些UITextField和一个" Save" UINavigationBar中的按钮。预期的行为如下:

当用户点击" Save"时,键盘必须隐藏并执行网络操作。在其回调中,显示了UIAlertView。当用户关闭此UIAlertView时,会发出通知并且当前的UIViewController会执行

[self.navigationController popViewControllerAnimated:YES];

问题是,如果" Save"按下键盘仍然显示,执行popViewControllerAnimated后,键盘会从左到右短暂出现(好像它在上一个UIViewController中可见)。

我已经尝试了

[myTextField resignFirstResponder]

当用户点击" Save",当用户解散UIAlertView时,甚至在

viewWillDisappear 

方法。其他一些答案建议使用

[self.view endEditing:YES];

但它也不起作用。

如果我可以使用常规键盘,覆盖

将是微不足道的
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}

在用户点击"返回","完成"等时隐藏它。但作为数字键盘不允许您显示"完成&# 34;按钮。

感谢您的帮助,谢谢大家的时间

3 个答案:

答案 0 :(得分:1)

试试这个:

将文本字段委托及其返回类型设置为Done,并将pad设置为数字键盘。

  _textField.delegate = self;
_textField.keyboardType = UIKeyboardTypeDecimalPad;
[_textField setReturnKeyType:UIReturnKeyDone];

现在将按钮添加到键盘:

    -(void)addButtonsToKeyboard
{
    UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
    [keyboardDoneButtonView sizeToFit];


    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil)
                                                                   style:UIBarButtonItemStyleDone target:self
                                                                  action:@selector(kbDoneAction:)];

    UIBarButtonItem* seperatorItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];


    UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil)
                                                                     style:UIBarButtonItemStylePlain target:self
                                                                    action:@selector(kbCancelAction:)];


    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:cancelButton,seperatorItem, doneButton, nil]];
    _textField.inputAccessoryView = keyboardDoneButtonView;
}

并隐藏键盘:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

您的完成操作方法是:

    -(void)kbDoneAction:(id)sender
{
    [_textField resignFirstResponder];
}

和取消操作方法是:

    -(void)kbCancelAction:(id)sender
{
[_textField resignFirstResponder];
    }

答案 1 :(得分:0)

我有类似的情况。我最终推迟了popViewControllerAnimated比键盘动画持续时间(0.333秒)更长的时间。

答案 2 :(得分:0)

Try using the below code. It works fine for iOS 8 and below version

    if (IS_OS_8_OR_LATER) {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *cancelAction = [UIAlertAction
                                       actionWithTitle:B_title
                                       style:UIAlertActionStyleCancel
                                       handler:^(UIAlertAction *action)
                                       {
                                           [self.navigationController popViewControllerAnimated:YES];

                                       }];
        [alertVC addAction:cancelAction];
        [self presentViewController:alertVC animated:YES completion:nil];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }
}
相关问题