Dalam Swift 4.1 dan Xcode 9.4.1
Solusinya adalah
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})
Jika menulis seperti ini saya mendapatkan kesalahan yang sama
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert.addAction(defaultAction)
present(alert, animated: true, completion: nil)
Saya mendapatkan kesalahan yang sama
Presenting view controllers on detached view controllers is discouraged <MyAppName.ViewController: 0x7fa95560Z070>.
Solusi lengkapnya adalah
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert.addAction(defaultAction)
//Made Changes here
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})