【问题标题】:Keyboard appearing briefly when popping viewcontroller iOS弹出viewcontroller iOS时键盘短暂出现
【发布时间】:2015-11-12 18:24:11
【问题描述】:

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

在我当前的 UIViewController 中。我在 UINavigationBar 中有几个 UITextField 和一个“保存”按钮。预期的行为如下:

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

[self.navigationController popViewControllerAnimated:YES];

问题是,如果在键盘仍然显示的情况下按下“保存”,则在执行 popViewControllerAnimated 后,键盘会从左到右短暂出现(就像在之前的 UIViewController 中可见一样)。

我试过了

[myTextField resignFirstResponder]

当用户点击“保存”时,当用户关闭 UIAlertView 时,甚至在

viewWillDisappear 

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

[self.view endEditing:YES];

但它也不起作用。

如果我可以使用常规键盘,覆盖将是微不足道的

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

    return YES;
}

当用户点击“返回”、“完成”等时隐藏它。但是作为数字键盘不允许您显示“完成”按钮。

非常感谢您的帮助,感谢大家的宝贵时间

【问题讨论】:

  • 您需要在数字键盘上添加完成/取消按钮。
  • 使用 [self.view endEditing:YES]; [self.navigationController popViewControllerAnimated:YES]之前;
  • @T_77 我试过 [myTextField setReturnKeyType:UIReturnKeyDone];它不起作用
  • @Dev 我也试过了,还是不行
  • 试试我的答案。我刚发了

标签: ios objective-c uiviewcontroller keyboard


【解决方案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;
}

你的 Done 操作方法是:

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

而取消动作方法是:

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

【讨论】:

  • 别忘了在 viewDidload 上调用 addButtons 方法
  • 还是不行。作为一种解决方法,我在调用 kDoneAction 之前禁用了保存按钮,但是在点击“保存”时它错过了隐藏它的意义
【解决方案2】:
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];
        }
}

【讨论】:

    【解决方案3】:

    我也遇到过类似的情况。我最终将popViewControllerAnimated 延迟了比键盘动画持续时间长一点(0.333 秒)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 2012-07-25
      相关资源
      最近更新 更多