简体   繁体   中英

Prevent view controllers from being presented when alert is there

I have some code that asynchronously checks for errors and if an error happens, it shows an alert using window.rootViewController?.present(...)

Sometimes, this happens right before a view controller is presented, so the alert is shown for a split second and then the new view controller goes on top of it.

The thing is that the new VC is also presented asynchronously (ie, when an HTTP request finishes).

Is there a way to make alerts really modal? How to approach this?

Perhaps this can help: What is the best way to check if a UIAlertController is already presenting?

I would try checking if the presentedViewController property is different from nil, even if it was presented a split second before, the property should contain the view controller that was presented. If that is the case, you avoid presenting a new one.

Another option is to add a view to an additional window above everything so that there is no way anything can be presented over it. You could present it at UIWindowLevelAlert or other level and it would be over all controllers. An example that I am pretty sure that is in its own window is the volume view in Instagram that slides down to cover the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM