2015-09-21 53 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"] 
相關問題