Attempting to load the view of a view controller while it is deallocating … UIAlertController

ぃ、小莉子 提交于 2019-11-29 22:11:28

I had the same issue with my UIViewController where i was only declaring variable in my class let alert = UIAlertView() without using it yet, it was out of all the functions just inside the class as variable. by removing that solves the issue. so please check in your class if you have defined alert from UIAlertView or UIAlertViewController like that without using it or in the class variable!

I was finally able to track it down to a UIActionSheet class variable inside a third-party library, Mapbox GL.

I opened an issue with that dev team: https://github.com/mapbox/mapbox-gl-native/issues/2475

Partial credit (and an up vote and bounty) to @Aaoli for mentioning having a UIAlertView as a class variable.

We had the same issue with UIAlertController.

let alert = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {(action : UIAlertAction!) in
                //some actions
                              } ))

I had forgot adding the following line. Below line solved the problem.

self.presentViewController(alert, animated: true, completion: nil)

I solved this by moving some of my code to viewDidAppear. If I used UIAlertController, it would cause the same problem you mentioned and would not be displayed, and I solved it the same way.

Let me know if that doesn't work!

In my case, in Swift 3, I had missed the code below after adding the action

presentViewController(theAlert, animated: true, completion: nil)

So, the working code is as below

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

     if (editingStyle == UITableViewCellEditingStyle.Delete) {

        let title = "Delete ????"
        let message = "Are you sure you want to delete this item?"

        let theAlert = UIAlertController(title: title,
                                   message: message,
                                   preferredStyle: .ActionSheet)

     let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
     theAlert.addAction(cancelAction)

     let onDelete = UIAlertAction(title: "Delete", style: .Destructive, handler: { (action) -> Void in
     self.items.removeAtIndex(indexPath.row)
     self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

     })
     theAlert.addAction(onDelete)
        presentViewController(theAlert, animated: true, completion: nil)
     }
     }

//Note I was using a sample array

var items = ["iPhone", "iPad", "Mac"]

I had the same issue, when I tried to remove a "frame" observer on my view in a UIViewController subclass. I solved this issue by wrapping the removeObserver in a isViewLoaded(). What exactly are you observing?

I had this problem by not having a navigationController... I know it sounds obvious but jumping about in various projects this one didn't have a UINavigationControllerto hand.... so others coming here you might want to NSLog your nav controller just for sanity too...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!