UIAlertController not in window hierarchy

Good morning guys,

when I'm calling UIAlertController (which is declared in separate class) from my ContentView, there is something wrong with window hierarchy - console throws this error:

[Presentation] Attempt to present <UIAlertController: 0x14400fe00> on <SWIFTSTEST.Alert: 0x14400c200> (from <SWIFTSTEST.Alert: 0x14400c200>) whose view is not in the window hierarchy.

Do you know what is wrong in here, or even better, could you shortly explain window hierarchy problematic?

Short describe of my code I'm typing under this message.

Thank you,


ContentView calls UIAlertController via Button:
struct ContentView: View {
let alert = Alert()
var body: some View {

UIAlertController is declared in separate class:
class Alert : UIAlertController {
let alert = UIAlertController(title: "My alert", message: "Hello!", preferredStyle: .alert)

func alertAction() {
    alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default"), style: .default, handler: { _ in NSLog("The \"OK\" alert occured.")
    self.present(alert, animated: true, completion: nil)


Since UIAlertController cannot be subclassed I simply created a function that returns the correctly configured UIAlertController inside the ViewController where it is needed.*