簡體   English   中英

在取消分配時嘗試加載視圖控制器的視圖... UIAlertController

[英]Attempting to load the view of a view controller while it is deallocating … UIAlertController

我正在使用Xcode 7.0的發布版本構建。 沒有故事板,只有nib文件。

我有一個由app delegate創建的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 ,但在收到此錯誤之前沒有使用或實例化。 這僅在iOS 9.0下運行時才會發生。 在iOS 8.4下運行不會產生錯誤。 在所有情況下,應用程序似乎正常運行,導航似乎正在工作。

我懷疑這個錯誤是誤導性的,但我怎么能解決這個問題呢?

Per @Nick,這是使用的dealloc方法:

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

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

我的UIViewController也有同樣的問題,我只在我的類中聲明變量let alert = UIAlertView()而不使用它,它只是在類中作為變量的所有函數。 通過刪除解決問題。 所以如果你沒有使用UIAlertViewUIAlertViewController或者在類變量中定義來自UIAlertViewUIAlertViewController警報,請檢查你的課程!

我終於能夠將它跟蹤到第三方庫Mapbox GL中的UIActionSheet類變量。

我向開發團隊提出了一個問題: https//github.com/mapbox/mapbox-gl-native/issues/2475

@Aaoli提到將UIAlertView作為類變量的部分功勞(以及投票和賞金)。

我們與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)

我通過將一些代碼移動到viewDidAppear來解決這個問題。 如果我使用了UIAlertController ,它會導致你提到的同樣的問題並且不會顯示,我以同樣的方式解決了它。

如果這不起作用,請告訴我!

就我而言,在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"]

當我試圖在UIViewController子類中移除我的視圖上的“框架”觀察者時,我遇到了同樣的問題。 我通過將removeObserver包裝在isViewLoaded()解決了這個問題。 你究竟在觀察什么?

我有這個問題沒有一套navigationController ......我知道這聽起來很明顯,但跳來跳去各種項目這一塊沒有一個UINavigationController手....所以其他人來這里,你可能想NSLog只是你的導航控制器為了理智...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM