UIAlertController console warning

Hi


I've recently update my app to use AlertControllers


However when I present one within a Navigation Controller i'm getting this warning in the console:

"presenting view controllers on detached view controllers is discouraged"


I'm assuming this is because the alert controller isn't added to the navigation controller stack.


It doens't seem to be breaking anything but is there something else I should be doing to avoid the warning?


Thanks


Chris

Answered by Floating Wrench in 99348022

What is "self" at the time you present the alertController which generates the warning? That is the view which is likely in the process of being presented or not in the current navigation stack.


If you want to track it down, I would start by setting breakpoints on these lines and examining the viewController referred to as "self" and its placement within the view hierarchy at that moment to gain an understanding of how your views are structured.


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


If you want a simple solution that just makes the warning go away. then you can use something like the following to always present from a known view controller, although I would recommend looking into things a bit more. A good understanding of how views are organized and presented in an application will be helpful in the long run.


[self.view.window.rootViewController presentViewController:viewController animated:YES completion:nil];

Well, you can probably eliminate the warning by following the suggestion and presenting the alert controller from a view that is in the navigation controller stack such as the rootViewController, for example.

Doing that from within viewWillAppear?

Apologies I'm probably showing my inexperience here.


The AlertControllers are used from views within my Navigation Stack and are called in response to a button press as confirmations to requests to delete information. Using the following:


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


Do I need to modify this to link correctly with my navigation stack?


Thanks


Chris

Accepted Answer

What is "self" at the time you present the alertController which generates the warning? That is the view which is likely in the process of being presented or not in the current navigation stack.


If you want to track it down, I would start by setting breakpoints on these lines and examining the viewController referred to as "self" and its placement within the view hierarchy at that moment to gain an understanding of how your views are structured.


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


If you want a simple solution that just makes the warning go away. then you can use something like the following to always present from a known view controller, although I would recommend looking into things a bit more. A good understanding of how views are organized and presented in an application will be helpful in the long run.


[self.view.window.rootViewController presentViewController:viewController animated:YES completion:nil];

Thanks for the explanation I really appreciate it.

UIAlertController console warning
 
 
Q