How to present UIAlertController when not in a view controller?

前端 未结 30 3604
庸人自扰
庸人自扰 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:11

    Shorthand way to do present the alert in Objective-C:

    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:nil];
    

    Where alertController is your UIAlertController object.

    NOTE: You'll also need to make sure your helper class extends UIViewController

提交回复
热议问题