3

I'm creating an alert but I can't dismiss it when the user presses OK. I'm getting the following error:

2017-12-28 07:03:50.301947-0400 Prestamo[691:215874] API error: <_UIKBCompatInputView: 0x10249adc0; frame = (0 0; 0 0); layer = > returned 0 width, assuming UIViewNoIntrinsicMetric

I was searching everywhere on the Internet but I couldn't find anything that helped me.

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

Any ideas would be appreciated

Wasim Malek
  • 123
  • 7
Chris
  • 111
  • 5

2 Answers2

1

You don't have to dismiss alert controller. It will automatically dismiss after the action handler has been called. Just remove the dismiss line.

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)
Bilal
  • 18,478
  • 8
  • 57
  • 72
  • Thank you for your answer. But I still have the same error/it doesn't dismiss itself. I didn't make any difference – Chris Dec 28 '17 at 11:21
0

Please note the difference with this answer and you code. You didn't invoked super.viewDidAppear(animated) and this causes problems.

Below code (this is all I used) works for me without problems (I've test it):

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)
    }
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Thank you for your answer. Xcode gives me a red error that presentViewController has been renamed to present. That is why I have present. The problems still remains – Chris Dec 28 '17 at 11:38