38

我正在使用 Xcode 7.0 的发布版本进行构建。没有故事板,只有 nib 文件。

我有一个UINavigationController由应用程序委托创建的,并使用视图控制器对其进行初始化。

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];

使用以下命令导航到新视图后:

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

然后返回使用:

[self.navigationController popViewControllerAnimated:YES];

我收到以下错误:

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

虽然我UIAlertController在应用程序中使用了 s,但在收到此错误之前没有使用或实例化。这仅在 iOS 9.0 下运行时发生。在 iOS 8.4 下运行不会产生错误。在所有情况下,该应用程序似乎都可以正常运行,并且导航似乎可以正常工作。

我怀疑该错误具有误导性,但我该如何解决这个问题?

根据@Nick,这是使用的 dealloc 方法:

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

- (void)dealloc {
    [self deregisterNotificationHandlers];
}
4

7 回答 7

47

我有同样的问题,我UIViewController只是在我的类中声明变量let alert = UIAlertView()而不使用它,它在类中的所有函数中都没有作为变量。通过删除它可以解决问题。因此,如果您在不使用它或在类变量中定义了警报UIAlertView或类似的警报,请检查您的课程!UIAlertViewController

于 2015-09-28T19:09:10.863 回答
6

我终于能够将其追踪到UIActionSheet第三方库 Mapbox GL 中的一个类变量。

我向该开发团队提出了一个问题:https ://github.com/mapbox/mapbox-gl-native/issues/2475

@Aaoli 提到将 aUIAlertView作为类变量的部分功劳(以及投票和赏金)。

于 2015-09-30T23:54:08.063 回答
5

我们遇到了同样的问题UIAlertController

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

我忘了添加以下行。下面的行解决了这个问题。

self.presentViewController(alert, animated: true, completion: nil)
于 2016-08-22T07:03:43.030 回答
4

我通过将我的一些代码移动到viewDidAppear. 如果我用UIAlertController了,会出现你提到的同样的问题,不会显示,我也是用同样的方法解决的。

如果这不起作用,请告诉我!

于 2015-09-27T08:05:31.250 回答
1

就我而言,在 Swift 3 中,我在添加操作后错过了下面的代码

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

所以,工作代码如下

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)
     }
     }

//注意我使用的是样本数组

var items = ["iPhone", "iPad", "Mac"]
于 2016-09-26T22:44:51.037 回答
0

当我尝试在子类的视图中删除“框架”观察者时,我遇到了同样的问题UIViewController。我通过removeObserverisViewLoaded(). 你到底在观察什么?

于 2015-09-27T19:24:34.157 回答
0

我没有一个navigationController... ...UINavigationControllerNSLog

于 2016-06-27T13:25:56.263 回答