xcode 在解除分配时尝试加载视图控制器的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32429185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:47:22  来源:igfitidea点击:

Attempting to load the view of a view controller while it is deallocating

iosxcodeswiftios9

提问by Latheesan

I am learning to implement TableView with Search Bar and data filtering. So, I have the following in my storyboard:

我正在学习使用搜索栏和数据过滤来实现 TableView。所以,我有以下内容storyboard

enter image description here

在此处输入图片说明

My UITableViewControlleris connected to the following SearchTableViewController.swiftclass like this:

UITableViewController的连接到以下SearchTableViewController.swift类是这样的:

import UIKit

class SearchTableViewController: UITableViewController, UISearchResultsUpdating {

    // table view controller properties
    let appleProducts = ["Mac", "iPhone", "Apple Watch", "iMac", "iPad"]
    var filteredAppleProducts = [String]()
    var resultSearchController = UISearchController()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Configure result search controller
        self.resultSearchController = UISearchController(searchResultsController: nil)
        self.resultSearchController.searchResultsUpdater = self
        self.resultSearchController.dimsBackgroundDuringPresentation = false
        self.resultSearchController.searchBar.sizeToFit()
        self.resultSearchController.searchBar.placeholder = "Search Products"
        self.tableView.tableHeaderView = self.resultSearchController.searchBar
        self.tableView.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.resultSearchController.active) {
            return self.filteredAppleProducts.count
        } else {
            return self.appleProducts.count
        }
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        if (self.resultSearchController.active) {
            cell.textLabel?.text = self.filteredAppleProducts[indexPath.row]
        } else {
            cell.textLabel?.text = self.appleProducts[indexPath.row]
        }

        return cell
    }

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        self.filteredAppleProducts.removeAll(keepCapacity: false)
        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
        let resultArray = (self.appleProducts as NSArray).filteredArrayUsingPredicate(searchPredicate)
        self.filteredAppleProducts = resultArray as! [String]
        self.tableView.reloadData()
    }

}

This runs fine and filtering is working as expected:

这运行良好,过滤按预期工作:

enter image description here

在此处输入图片说明

However, when the app is running in the simulator, I get the following warning in the debug window:

但是,当应用程序在模拟器中运行时,我在调试窗口中收到以下警告:

2015-09-07 00:02:46.116 TableViewSearch[2415:68217] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior ()

2015-09-07 00:02:46.116 TableViewSearch[2415:68217] 不允许在解除分配时尝试加载视图控制器的视图,这可能会导致未定义的行为 ()

Any ideas how to fix this?

任何想法如何解决这一问题?

回答by MichikoP

I spent all day to understand this problem too... Some said it's related to Navigation Controller issue and others said it's a glitch. Although this may not be the correct answer, hope I can give some hint. If you think it's not a correct approach, let me know. Thanks. My case is as follows instead (because of a different design, I think.)

我也花了一整天的时间来理解这个问题......有人说它与导航控制器问题有关,其他人说这是一个小故障。虽然这可能不是正确的答案,但希望我能给一些提示。如果您认为这不是正确的方法,请告诉我。谢谢。我的情况如下(我认为是因为设计不同。)

var resultSearchController: UISearchController!()

Then, I had to add below.

然后,我不得不在下面添加。

self.resultSearchController.loadViewIfNeeded()

回答by Apoorv Mote

Just change following line

只需更改以下行

var resultSearchController = UISearchController()

to following line

到以下行

var resultSearchController:UISearchController!

Your rest of the code is ok. Only that one change is required.

你的其余代码没问题。只需要进行一项更改。

If you want to see detailed explanation then check my tutorial on UISearchController

如果您想查看详细说明,请查看我在 UISearchController 上的教程