UIAlertController的警告无法解除它

时间:2017-12-28 11:08:37

标签: swift

我正在创建提醒,但是当用户按下“确定”时我无法将其解除。我收到以下错误:

  

2017-12-28 07:03:50.301947-0400 Prestamo [691:215874] API错误:   < _UIKBCompatInputView:0x10249adc0; frame =(0 0; 0 0); layer =   >返回0宽度,假设   UIViewNoIntrinsicMetric

我在互联网上到处搜索,但我找不到任何帮助我的东西。

   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)
}

任何想法都将不胜感激

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

请注意与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)
    }
}