2015-09-21 51 views
38

我正在与发布版本的Xcode 7.0建设。没有故事板,只是笔尖文件。尝试加载视图控制器的视图,当它正在释放... UIAlertController

我有一个由应用程序委托创建的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>) 

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

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

每@Nick,这里是正在使用的dealloc方法:

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

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

我强烈怀疑哟你在某个地方使用UIAlertController,并没有意识到这一点,也不打算这么做。在提供UIAlertControllers的地方设置断点并查看会发生什么 – Paulw11

+0

我在+ [UIAlertController alertControllerWithTitle:message:preferredStyle:]上设置了断点,但没有设置断点。我后来创建了一个警报,断点确实起作用。 – picciano

+0

你是否在任何地方重写'dealloc'?你能发布更多的代码吗? – Nick

回答

6

我终于可以在第三方库Mapbox GL中追踪到UIActionSheet类变量。

我打开与开发团队的一个问题:https://github.com/mapbox/mapbox-gl-native/issues/2475

部分信贷(和上选票和奖金),以用于@Aaoli提到有UIAlertView为类变量。

+0

在我的情况下,同样的错误显示在控制台上的视图包含Mapbox MGLMapView – Nazir

4

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

让我知道如果这不起作用!

+0

谢谢,但我没有实例化任何'UIAlertController'实例,但是这个错误记录到控制台。 – picciano

+0

是的,它不仅适用于'UIAlertController'。如果将所有视图的初始化代码移动到'viewDidAppear',会发生什么? – Sheamus

+0

谢谢你的工作! – VivienG

0

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

+1

谢谢,但我没有使用框架观察员。 – picciano

46

我与我的UIViewController有同样的问题,我只在我的类let alert = UIAlertView()中声明了变量,但尚未使用它,它只是在类内作为变量的所有函数。通过消除解决问题。所以请检查你的班级,如果你已经定义了UIAlertViewUIAlertViewController这样的警报,而不使用它或在类变量!

+1

谢谢,但我没有'UIAlertView'或'UIAlertViewController'的类变量。 – picciano

+0

@picciano,再次检查你的定义在视图加载之前或其他地方......这个消息是由此引起的!尝试搜索投掷项目并禁用任何警报使用!几天前我有这个错误,我知道它有什么关于!当您将控制器推送到警报声明的位置时,会发生这种情况。 – AaoIi

+0

谢谢。哇,这是违反直觉的。我在局部变量中定义了一个UIAlertView,然后在某些情况下没有出现。我找不到什么不对,救了我的一天;) – Laky

0

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

4

我们遇到了与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) 
1

在我的情况下,斯威夫特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"] 
相关问题