ios 试图在解除分配时加载视图控制器的视图... UIAlertController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32704169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:46:16  来源:igfitidea点击:

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

iosswiftxcodexcode7mapbox-gl

提问by picciano

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

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

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

我有一个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];

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.

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

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

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

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

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

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

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

采纳答案by picciano

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

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

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

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

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

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

回答by AaoIi

I had the same issue with my UIViewControllerwhere 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 UIAlertViewor UIAlertViewControllerlike that without using it or in the class variable!

我有同样的问题,我UIViewController只是在我的类中声明变量let alert = UIAlertView()而不使用它,它在类中作为变量的所有函数之外。通过删除解决问题。所以请检查你的班级是否已经定义了警报UIAlertViewUIAlertViewController喜欢它而不使用它或在类变量中!

回答by Photon Point

We had the same issue with UIAlertController.

我们有同样的问题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)

回答by Sheamus

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.

我通过将一些代码移动到viewDidAppear. 如果我使用UIAlertController,它会导致您提到的相同问题并且不会显示,我以同样的方式解决了它。

Let me know if that doesn't work!

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

回答by Naishta

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

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

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

回答by fruitcoder

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

我遇到了同样的问题,当我试图在UIViewController子类中的视图中删除“框架”观察者时。我通过包装来解决这个问题removeObserverisViewLoaded()。你究竟在观察什么?

回答by Magoo

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 NSLogyour nav controller just for sanity too...

我有这个问题是因为没有navigationController...我知道这听起来很明显但是在各种项目中跳来跳去这个没有UINavigationController手...所以其他人来到这里你可能也想要NSLog你的导航控制器只是为了理智...