Why: “Attempting to load .. while deallocating… :U

2019-05-03 09:34发布

问题:

I have root storyboard that has a button that pushes ViewControllerB.

ViewControllerB has a sort controller UISortController.

ViewControllerB has a "back" method that is controlled by the root nav controller.

I'm getting the following warning:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController: 0x7ff10258ba60>)

I used Apple's sample (membership required) to add the new UISearchController.

Has anyone come across this? How do I resolve it?

回答1:

I would recommend using Storyboard Unwind Segues instead: https://developer.apple.com/library/ios/technotes/tn2298/_index.html

This insightful post also has a wealth of very useful implementation detail:

What are Unwind segues for and how do you use them?



回答2:

I've come across the same console error message, but note the following regarding my Xcode project:

  • segues to navigation controllers must remain due to their use with a split view controller (to the detail view controller);
  • (and according to @AndrewEbling comments above) the code does not require unwind segues as the navigation controller manages (a proportion of) the tasks automatically;
  • also the code does not require unwind segues as blocks are used to set properties in parent view controller.

Thanks to comments by Leehro and Clafou in this SO answer the console error message was suppressed by using following line of code in viewDidLoad (note that there is no need to insert this in dealloc):

[self.searchController loadViewIfNeeded]; (note available from iOS 9).