【问题标题】:alert with UIAlertController can't dissmiss it带有 UIAlertController 的警报无法解除它
【发布时间】:2018-06-08 22:27:44
【问题描述】:

我正在创建一个警报,但当用户按下“确定”时我无法将其关闭。我收到以下错误:

2017-12-28 07:03:50.301947-0400 Prestamo[691:215874] API 错误: <_uikbcompatinputview:> 返回 0 宽度,假设 UIViewNoIntrinsicMetric

我在 Internet 上到处搜索,但找不到任何对我有帮助的东西。

   override func viewDidAppear(_ animated: Bool) {
   createAlert(title: "Licencia2", message: "En el momento no tienes una licencia válida!")
}

   func createAlert (title:String, message:String) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(action) in
        alert.dismiss(animated: false, completion: nil)
    }))
    self.present(alert, animated: false, completion: nil)
}

任何想法将不胜感激

【问题讨论】:

  • 不是重复的。我的警报母猪,但之后我无法关闭它。
  • IMO 问题出在 viewDidAppear 中,它不会调用 super.viewDidAppear(animated)
  • 我现在添加了 super.viewDidAppear(animated) 并没有任何区别
  • 可能你已经添加了两次警报。

标签: swift


【解决方案1】:

您不必关闭警报控制器。调用操作处理程序后,它将自动关闭。只需删除解雇线。

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(action) in

}))
self.present(alert, animated: false, completion: nil)

【讨论】:

  • 感谢您的回答。但我仍然有同样的错误/它不会自行解散。我没有任何区别
【解决方案2】:

请注意与this answer 和您的代码的区别。 您没有调用super.viewDidAppear(animated),这会导致问题。

下面的代码(这是我使用的全部)对我来说没有问题(我已经测试过):

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        createAlert(title: "My test", message: "THis should work ok")
    }

    func createAlert (title:String, message:String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default) {
            action in
            NSLog("it is working ok");
        })

        present(alert, animated: true)
    }
}

【讨论】:

  • 感谢您的回答。 Xcode 给了我一个红色错误,表示 presentViewController 已重命名为 present。这就是我在场的原因。问题依然存在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-17
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
相关资源
最近更新 更多