Звучит банально, я много раз делал Алерт все отображалось, но тут не могу понять, почему не отображается?
Делал разными способами:
**Просто вставил в класс и запускал в методе viewDidLoad() :
class MainScreenTVC: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
newUpdatesAvailable()
}
override func newUpdatesAvailable() {
let alert = UIAlertController(title: "Доступны обновления", message: "Вы хотите их скачать?", preferredStyle: .alert)
let okAlertAction = UIAlertAction(title: "OK", style: .default) { (action : UIAlertAction) in
if isInternetAvailable() {
print("Internet connection OK")
self.refreshData()
} else {
print("Internet connection FAILED")
self.failedConnectionInternet()
}
}
let cancelAlertAction = UIAlertAction(title: "Отмена", style: .cancel) { (action : UIAlertAction) in
self.downLoadNewVersionButton.setTitle("Доступны обновления!", for: .normal)
}
alert.addAction(okAlertAction)
alert.addAction(cancelAlertAction)
present(alert, animated: true, completion: nil)
}
}
Через extension
extension UIViewController {
func newUpdatesAvailable() {
let alert = UIAlertController(title: "Доступны обновления", message: "Вы хотите их скачать?", preferredStyle: .alert)
let okAlertAction = UIAlertAction(title: "OK", style: .default) { (action : UIAlertAction) in
let pathTorefreshData = UIApplication.shared.delegate as! MainScreenTVC
if isInternetAvailable() {
print("Internet connection OK")
pathTorefreshData.refreshData()
} else {
print("Internet connection FAILED")
pathTorefreshData.failedConnectionInternet()
}
}
let cancelAlertAction = UIAlertAction(title: "Отмена", style: .cancel) { (action : UIAlertAction) in
let pathTorefreshData = UIApplication.shared.delegate as! MainScreenTVC
pathTorefreshData.downLoadNewVersionButton.setTitle("Доступны обновления!", for: .normal)
}
alert.addAction(okAlertAction)
alert.addAction(cancelAlertAction)
present(alert, animated: true, completion: nil)
}
}
Создавал отдельный класс:
class AlertHelper {
func showAlert(fromController controller: UIViewController) {
var alert = UIAlertController(title: "abc", message: "def", preferredStyle: .Alert)
controller.presentViewController(alert, animated: true, completion: nil)
}
}
Вызывал
var alert = AlertHelper()
alert.showAlert(fromController: self)
Появляется одна и даже ошибка
Warning: Attempt to present <UIAlertController: 0x7fe4a2d24130> on <rusPddRules.MainScreenTVC: 0x7fe4a2d195b0> whose view is not in the window hierarchy!
Из ошибки понятно, что неправильная иерархия, но куда тут не правильная. хз