简体   繁体   中英

How do i check if another view controller is visible?

Sometimes my app crashes when two UIAlertViews are trying to be presented at the same time. How do I check if another UIAlertview is being shown before showing another one?

Here is what I tried, but it doesn't work.

Note: I'm using swift for this project.

if ViewController.isVisable == false {    
    self.presentViewController(AlertView, animated: true, completion: nil)    
}

As a strict response to your question you could check if the ViewController 's view has his window:

if ViewController.view.window != nil {

}

Anyway I encourage you to take @Wain ' s advice.

You shouldn't be approaching the problem like this. Instead, whatever is triggering the alert to be shown should be enabled / disabled as the view controllers view is shown / hidden. A view controller which isn't on display generally shouldn't be consuming resources by continuing to track state and trying to update the UI (either in its own view or using alerts).

There are obviously exceptions to this sometimes, but it should be your baseline position and will avoid issues like multiple alert views at the same time (or at least for the same reason / trigger).

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