0

When presenting an UIAlertController is it necessary to use the top most UIViewcontroller on the screen? Does this UIViewController need to be visible or just in the view hierarchy? Would there be any disadvantage if presenting any alert directly from the UIWindow's root viewController?

Lescai Ionel
  • 4,216
  • 3
  • 30
  • 48

2 Answers2

1

In my opinion there is nothing wrong in presenting from the root view controller of the application. Take a look at the following question and the accepted answer How to present UIAlertController when not in a view controller?

I've used a category on UIAlertViewController similar to what have been proposed in the answers in the above link, and I've never faced any problem.

Community
  • 1
  • 1
Shayan Jalil
  • 588
  • 5
  • 17
1

I think it's better to present the UIAlertViewController from the currently presented view controller. For me I used to get the currently presented view controller like this:

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
UIViewController *viewController = keyWindow.rootViewController;
while (viewController.presentedViewController) {
    viewController = viewController.presentedViewController;
}
[viewController presentViewController:alertController animated:YES completion:nil]

Using the root to present the view controller caused an issue for me before but I can't remember actually. You're at the safe side while using the top most one.

Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64