show UIAlertController outside of ViewController

前端 未结 4 2144
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 07:43

I have trouble to display my UIAlertController because I\'m trying to show it in a Class which is not an ViewController.

I already tried adding it:

 var          


        
4条回答
  •  遇见更好的自我
    2021-02-01 08:42

    If you solution is not working it probably because of there is no window at that moment. I had the same problem when I was trying to show alert view in application:DidFinishLoadingWithOptions method. In this case my solution was to check if root view controller is available, and if it's not, then add notification for UIApplicationDidBecomeActiveNotification

    NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification,
                    object: nil,
                    queue: NSOperationQueue.mainQueue()) {
                        (_) in
                            //show your alert by using root view controller
                            //remove self from observing
                        }
            }
    

提交回复
热议问题