ViewWillDisappear未被调用searchcontroller

9

当我正在搜索并切换UI选项卡时,ViewWillDisappear不会被调用。你知道为什么在我筛选结果显示并切换选项卡时ViewWillDisappear不会被调用吗?

func updateSearchResultsForSearchController(searchController: UISearchController) {
  if self.searchController?.searchBar.text.lengthOfBytesUsingEncoding(NSUTF32StringEncoding) > 0 {
        if let results = self.results {
            results.removeAllObjects()
        } else {
            results = NSMutableArray(capacity: MyVariables.dictionary.keys.array.count)
        }

        let searchBarText = self.searchController!.searchBar.text

        let predicate = NSPredicate(block: { (city: AnyObject!, b: [NSObject : AnyObject]!) -> Bool in
            var range: NSRange = NSMakeRange(0, 0)
            if city is NSString {

                range = city.rangeOfString(searchBarText, options: NSStringCompareOptions.CaseInsensitiveSearch)
            }

            return range.location != NSNotFound
        })

        // Get results from predicate and add them to the appropriate array.
        let filteredArray = (MyVariables.dictionary.keys.array as NSArray).filteredArrayUsingPredicate(predicate)
        self.results?.addObjectsFromArray(filteredArray)


        // Reload a table with results.
        self.searchResultsController?.tableView.reloadData()
    }
}




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

    var text: String?
    var imgtext:AnyObject?
    if tableView == self.searchResultsController?.tableView {
        if let results = self.results {
            text = self.results!.objectAtIndex(indexPath.row) as? String
           imgtext = MyVariables.dictionary[text!]
            let decodedData = NSData(base64EncodedString: imgtext! as! String, options: NSDataBase64DecodingOptions(rawValue: 0) )
            var decodedimage = UIImage(data: decodedData!)


            cell.imageView?.image = decodedimage
        }
    } else {
        text = MyVariables.dictionary.keys.array[indexPath.row] as String
    }

    cell.textLabel!.text = text


    return cell
}

在负载中
  override func viewDidLoad() {
    super.viewDidLoad()

    let resultsTableView = UITableView(frame: self.tableView.frame)
    self.searchResultsController = UITableViewController()
    self.searchResultsController?.tableView = resultsTableView
    self.searchResultsController?.tableView.dataSource = self
    self.searchResultsController?.tableView.delegate = self

    // Register cell class for the identifier.
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier)
    self.searchResultsController?.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier)

    self.searchController = UISearchController(searchResultsController: self.searchResultsController!)
    self.searchController?.searchResultsUpdater = self
    self.searchController?.delegate = self
    self.searchController?.searchBar.sizeToFit()
    self.searchController?.hidesNavigationBarDuringPresentation = false;
    self.tableView.tableHeaderView = self.searchController?.searchBar
    self.definesPresentationContext = true

}

你要检查哪个视图控制器的viewWillDisappear?是主表视图控制器还是searchResultsController? - pbasdf
@pbasdf UITableViewController - user5130344
1
@user5130344,我也遇到了同样的问题?你找到任何解决方案了吗? - Saty
3个回答

14

我曾经遇到过同样的问题。在UITableViewController中,viewWillDisappear方法没有被调用,但是在UISearchController中会被调用。

所以我建立了UISearchController的子类并重写了viewWillDisappear方法。在我的情况下,我只需要让搜索控制器停用即可。

class SearchController: UISearchController {

    override func viewWillDisappear(_ animated: Bool) {        
        // to avoid black screen when switching tabs while searching
        isActive = false
    }
}

0

和@user5130344类似,我发现子类化解决了我的问题,尽管我发现isActive = false清除了搜索栏,而我希望搜索查询在返回视图时仍然保留。

这是我的子类 - 这修复了iOS 13关闭父视图的问题:

class MySearchController: UISearchController {

    override func viewWillDisappear(_ animated: Bool) {
        // to avoid black screen when switching tabs while searching
        self.dismiss(animated: true)
    }
}

-6

我认为这是Xcode的问题。尝试关闭它,重新打开项目,然后再次尝试运行。


1
这不是Xcode的问题,我已经重新编译了好几次,并关闭并重新打开了Xcode数十次...我的代码中有些东西阻止页面被释放,但我无法找出是什么。 - user5130344
尝试注释掉你觉得有点可疑的代码(例如:与搜索相关的代码),然后尝试在选项卡之间导航,看看问题是否仍然存在,因为我无法找到问题所在。 - sriram hegde

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接