AlertController is not in the window hierarchy

前端 未结 8 1765
北荒
北荒 2020-11-29 03:01

I\'ve just created a Single View Application project with ViewController class. I would like to show a UIAlertController from a function which is located inside my own class

8条回答
  •  悲哀的现实
    2020-11-29 03:17

    Write the following 3 lines, all we need to do is this.

    Swift 3.0

    private func presentViewController(alert: UIAlertController, animated flag: Bool, completion: (() -> Void)?) -> Void {
         UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: flag, completion: completion)
      }
    

    Swift 2.0

      private func presentViewController(alert: UIAlertController, animated flag: Bool, completion: (() -> Void)?) -> Void {
         UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alert, animated: flag, completion: completion)
      }
    

提交回复
热议问题