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

I am building with the release version of Xcode 7.0. No storyboards, just nib files.

I have a single UINavigationController created by the app delegate and initialize it with a view controller.

  • How to speed up UI test cases in Xcode?
  • CGContextSaveGState: invalid context 0x0 Error only on device
  • Xcode UI Tests can't find views that are added programatically
  • Linking error when building Parse in Xcode 7
  • How to install iOS 7 and onwards simulators in Xcode 7 Beta 5?
  • What is nonnull in objective C?
  • self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIViewController *viewController = [[TGMainViewController alloc] initWithNibName:nil bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.hidden = YES;
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    After navigating to a new view using:

    TGRoutePreparationViewController *viewController = [[TGRoutePreparationViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];

    Then going back using:

    [self.navigationController popViewControllerAnimated:YES];

    I receive the following error:

    Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x7b29a600>)

    While I do use UIAlertControllers in the app, none are used or instantiated before receiving this error. This only happens when running under iOS 9.0. Running under iOS 8.4 produces no error. In all cases, the app appears to function normally and the navigation appears to be working.

    I suspect the error is misleading, but how can I fix this?

    Per @Nick, here is the dealloc method being used:

    - (void)deregisterNotificationHandlers {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    - (void)dealloc {
        [self deregisterNotificationHandlers];

    7 Solutions Collect From Internet About “Attempting to load the view of a view controller while it is deallocating … UIAlertController”

    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:

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

    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!

    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)

    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)
         let onDelete = UIAlertAction(title: "Delete", style: .Destructive, handler: { (action) -> Void in
         self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
            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…