I have the following code in my Swift 3 project's main ViewController.swift file. When I call showAgreement(), then it executes without error, but no UI is displayed. How can I get UIAlertController to display?
My ViewController.swift file is defined as:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.showAgreement()
}
var alert: UIAlertController?
func showAgreement() {
alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert!.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
alert!.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}
}))
self.present(alert!, animated: true, completion: {
})
}
}
The console output says:
Warning: Attempt to present on whose view is not in the window hierarchy!