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

Ios 试图在视图控制器解除分配时加载视图控制器的视图。。。UIAlertController,ios,swift,xcode,xcode7,mapbox-gl,Ios,Swift,Xcode,Xcode7,Mapbox Gl,我正在使用Xcode 7.0的发行版进行构建。没有故事板,只有nib文件 我有一个由app委托创建的UINavigationController,并用视图控制器初始化它 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIViewController *viewController = [[TGMainViewController alloc] initWithNibName:nil b

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

我有一个由app委托创建的
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>)

我通过将一些代码移动到
viewdide
解决了这个问题。如果我使用了
UIAlertController
,它将导致与您提到的相同的问题,并且不会显示出来,我用相同的方法解决了它


如果不行,请告诉我

当我试图在
UIViewController
子类中删除视图上的“帧”观察者时,我也遇到了同样的问题。我通过将
removeObserver
包装到
isViewLoaded()
中解决了这个问题。你到底在观察什么?

我的
UIViewController
也有同样的问题,我只是在我的类中声明变量
let alert=UIAlertView()
而没有使用它,它是作为变量出现在类中的所有函数中。通过移除,解决了问题。因此,如果您已经从
UIAlertView
UIAlertViewController
中定义了类似的警报,而没有使用它或在类变量中,请检查您的类

我终于能够在第三方库Mapbox GL中找到
UIActionSheet
类变量

我向该开发团队提出了一个问题:


@Aaoli提到将
UIAlertView
作为类变量,这部分归功于@Aaoli(以及向上投票和奖励)。

我因为没有
navigationController而遇到了这个问题。。。我知道这听起来很明显,但在各种各样的项目中,这一个手头没有
UINavigationController
。。。。因此,其他人来到这里,您可能想
NSLog
您的导航控制器也只是为了保持理智…

我们对
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)

在我的例子中,在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"]

我强烈怀疑您正在某处使用UIAlertController,但没有意识到,也不打算这样做。在显示UIAlertController的位置设置断点,并查看发生的情况我在+[UIAlertController alertControllerWithTitle:message:preferredStyle:]上设置了断点,但没有断点。我后来创建了一个警报,断点也起作用了。您是否在任何地方覆盖
dealloc
?你能发布更多的代码吗?很多类都有一个dealloc方法来取消注册通知。我会在上面加上。完全脱离主题,但是。。。如果您的方法
-deregisterNotificationHandlers
所做的只是调用
+[NSNotificationCenter removeObserver:
,也许您可以直接在
-dealloc
中执行,并删除多余的方法…谢谢,但我没有
UIAlertView
UIAlertViewController
@picciano的类变量,再次检查您的“查看前定义”加载的地方或其他地方。。。此消息是由此引起的!尝试搜索并抛出项目,并禁用您使用的任何警报!我几天前就犯了这个错误,我知道这是怎么回事!当您将控制器推到警报声明的位置时,会发生此情况。谢谢。哇,这是违反直觉的。我在局部变量中定义了UIAlertView,但在某些情况下没有显示它。我找不到问题所在,救了我一天;)在我的例子中,问题是UIActionSheet作为实例变量+1@NikitaP,可能是您在删除视图或视图控制器时显示警报!我建议您使用top view controller(您将在stackoverflow中找到),然后在该top controller上显示警报!谢谢,但我没有使用帧观察器。谢谢,但我没有实例化任何
UIAlertController
实例,但此错误会记录到控制台。是的,它不仅仅涉及
UIAlertController
。如果将所有视图的初始化代码移动到
viewdide
,会发生什么情况?在我的情况下,控制台中显示的视图包含映射框MGLMapViewSwift 4-使用-存在的相同错误(控制器,动画:true,完成:nil)
var items = ["iPhone", "iPad", "Mac"]