How do I remove the deprecated UIAlertView from my code without significant refactoring?

108 views Asked by At

I have an objective-C app running on iOS. When I wrote it originally I used numerous UIAlerts to communicate with the user, but these are now deprecated, so I need to change to UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.

This seems relatively simple, except that I now need to present the UIAlertController to the current UIViewController. To do that, I need to know what the current UIViewController is.

This is a problem for an alert that is displayed by a separate class that is not itself part of the display stack. I can’t just say:

    [self presentViewController:alert animated:YES completion:nil];

because self here doesn’t have a presentViewController method.

I see two possible solutions, but I don’t like either of them, for the reasons given:

  1. Find the current UIRootViewController by using the code suggested (thank you!) by https://stackoverflow.com/users/4720315/mannam-brahmam, i.e.

     UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
     [vc presentViewController:alert animated:YES completion:nil];
    

    This works okay, but seems unnecessarily complicated, given that before UIAlert was deprecated, it was completely unnecessary (I just declared the alert and then did [alert show]; without regard to what the current viewController was).

  2. Stop issuing alerts from a subsidiary class altogether - return a status code and have the invoking viewController issue the alert instead.

    This may actually be a better design, so maybe I should have started out like this, but at this point it would involve a lot of refactoring.

Is there a third option that I’m not seeing?

Why would Apple deprecate UIAlertView if it causes this amount of refactoring?

0

There are 0 answers