【问题标题】:UIAlertView displaying keyboard, not able to dismiss itUIAlertView 显示键盘,无法关闭它
【发布时间】:2015-04-22 22:04:28
【问题描述】:

我有一个在其登录窗口中正常使用 UIAlertView 的应用程序:

self.customAlert = [[IFCustomAlertView alloc] initWithTitle:@"Save Password"
                                                                message:@"¿Do you want the app to remember your password?"
                                                               delegate:self
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:@"Cancel", nil];

问题是......因为我将我的设备更新到 iOS8,每当这个 alertView 出现时,它就会显示键盘,我无法关闭它。在 iOS7 上不会发生这种情况。

点击发送按钮时,我将退出用户和密码的响应者:

-(IBAction)btnSendTapped:(id)sender{
    [self.tfpass resignFirstResponder];
    [self.tfuser resignFirstResponder];
}

我试过了:

[self.view endEditing:YES];

在某些 alertViews 中它确实有效,但在另一些中则无效。我的 AlertViews 从来没有文本字段,所以我认为这个键盘没有理由出现。

键盘上的介绍按钮也不会隐藏它,所以有时确定和取消按钮被键盘挡住了,我在屏幕上什么也做不了。

我认为这可能与 UIAlertView 弃用有关,但我不知道。

我也实现了这些方法:

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

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

    return YES;
}

任何帮助将不胜感激。

【问题讨论】:

  • 我看到您使用的是IFCustomAlertView。尝试检查您的IFCustomAlertView 与默认UIAlertView 有何不同
  • 它只是有一些设计定义,具体取决于我显示的 UIAlertView 的类型,但不会影响代码。奇怪的是它只是在 iOS8 上发生

标签: ios objective-c keyboard uialertview uialertcontroller


【解决方案1】:

我从this博客借用解决方案

对我来说,当调用 alertView.show() 时,键盘总是会出现。

我的解决方案是使用 didPresentALertView 方法来确保在弹出警报视图后调用该方法。然后我们可以遍历所有 UIWindows 和它的子视图。我通过描述名称检测它(如果需要,您可以使用更准确的方法),只需将其从 superview 中删除即可。

func didPresentAlertView(alertView: UIAlertView) {
    var tempWindow: UIWindow;
    var keyboard: UIView;

    for var c = 0; c < UIApplication.sharedApplication().windows.count; c++ {
        tempWindow = UIApplication.sharedApplication().windows[c] as! UIWindow
        for var i = 0; i < tempWindow.subviews.count; i++ {
            keyboard = tempWindow.subviews[i] as! UIView

            println(keyboard.description)

            if keyboard.description.hasPrefix("<UIInputSetContainerView") {
                keyboard.removeFromSuperview()
            }
        }
    }
}

希望有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 2016-03-04
    相关资源
    最近更新 更多