How to present UIAlertController when not in a view controller?

前端 未结 30 3160
庸人自扰
庸人自扰 2020-11-22 06:21

Scenario: The user taps on a button on a view controller. The view controller is the topmost (obviously) in the navigation stack. The tap invokes a utility class method call

30条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 07:03

    You can do the following with Swift 2.2:

    let alertController: UIAlertController = ...
    UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
    

    And Swift 3.0:

    let alertController: UIAlertController = ...
    UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
    

提交回复
热议问题