【问题标题】:While displaying an alert in IOS, keyboard does not resign from View在 IOS 中显示警报时,键盘不会从视图中退出
【发布时间】:2015-11-12 11:34:43
【问题描述】:

在IOS 8中显示警告(密码错误)时,键盘会自动打开并隐藏alert(仅在Iphone 4s中,因为屏幕大小),所以我不能点击“确定”,我也可以不要关闭键盘,因为首先我需要关闭alert

Keyboard hides alert

(应用程序似乎正在恢复上次键盘的状态并再次显示)

如何在调用alert之前关闭键盘?(这样状态会“关闭”)

我试过了:

myTextField!.resignFirstResponder()

在调用按钮时,但它不起作用,alert 出现并自动打开键盘!

【问题讨论】:

    标签: ios swift keyboard alert resignfirstresponder


    【解决方案1】:

    如果myTextField!.resignFirstResponder() 无法正常工作,请在显示警报时尝试此操作,然后再调用此操作 -->self.view.endEditing(true)

    上面的功能不好用,试试

    选择 -1:使用响应者链

    UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
    

    这将使第一响应者每次都退出(并关闭键盘),而无需将 resignFirstResponder 发送到正确的视图。无论如何,这都会关闭键盘。这是迄今为止最好的方法:不用担心第一响应者是谁

    选择-2:UIView的endEditing

    (假设您的文本字段是您调用它的视图的子视图)。大多数时候:

    self.view.endEditing(true)
    

    【讨论】:

    • 在我的选择中使用 UIAlertController
    • 我正在尝试 UIAlertController,我认为它会起作用,但是我如何从 NSObject 调用 UIAlertController? “self.presentViewController(alertController, animated: true, completion:nil)” 不起作用!!错误消息显示“aleta 没有名为“presentViewController”的成员
    • 设置如 UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alerta, animated: true, completion: nil)
    • @FelipeF -- 这是另一个问题,如果版主看到这个也删除答案和问题,好的让我们试试这个链接stackoverflow.com/questions/24223427/…
    • @FelipeF -- 在之前使用 UIalertview 中尝试一次并尝试我的答案,回复我你是否得到答案,获取 UIalertviewcontroller,我们将继续下一步
    【解决方案2】:

    Delegate 设置为myTextField

    包括

    func textFieldShouldReturn(textField: UITextField) -> Bool
        {
            textField .resignFirstResponder()
            return true
        }
    

    其他明智的尝试以下方法

    var activeField : UITextField!
    
        func textFieldDidBeginEditing(textField: UITextField)
        {
            activeField = textField
        }
        func textFieldDidEndEditing(textField: UITextField)
        {
            activeField = nil
        }
        func textFieldShouldReturn(textField: UITextField) -> Bool
        {
            textField .resignFirstResponder()
            return true
        }
    

    在警报出现之前致电activeField.resignFirstResponder()

    【讨论】:

      【解决方案3】:

      我认为从 iOS8 开始你需要使用 UIAlertController 而不是 UIAlertView。在 iOS8 及更高版本中使用 UiAlertView 会导致键盘不必要地弹出。我已经看到了这一点,并且我做了一个条件,将 UIAlertController 用于 iOS8 及更高版本。在以下版本中 UIAlertView 应该可以正常工作

      【讨论】:

      • 我正在尝试 UIAlertController,我认为它会起作用,但是我如何从 NSObject 调用 UIAlertController? self.presentViewController(alertController, animated: true, completion:nil) 行“self.presentViewController(alertController, animated: true, completion:nil)”不起作用!!错误消息显示“aleta 没有名为“presentViewController”的成员
      • self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil) 试试这个
      • 没有名为“window”的成员
      • 你有 git 链接吗?顺便说一句,您的视图控制器继承自 UiViewController 对吗?
      • 好吧,基本上当我尝试使用代码时 UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewCont‌​‌​roller(alerta, animated: true, completion: nil) 错误:( UIViewController没有名为 'presentViewController' 的成员),当我尝试时:self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 错误:(Alerta 没有名为 'window' 的成员)我'我很确定这是关于继承的!我正在尝试修复它,并在 stackoverflow 中查找一些示例。无论如何,我确信 UIAlertController 是最好的方法!
      【解决方案4】:

      这是 iOS 8 上的 UIAlertView 错误。 我有同样的问题,但 UIAlertController 没有问题。 :3

      UIAlertView 自 iOS8 以来已被弃用。

      https://developer.apple.com/reference/uikit/uialertview

      【讨论】:

        【解决方案5】:

        在 Swift 4 中:下面的代码对我有用

            DispatchQueue.main.async() {
                self.view.endEditing(true)
            }
        

        【讨论】:

          猜你喜欢
          • 2013-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多