ios - ViewWillDisappear 没有被调用 searchcontroller

标签 ios objective-c xcode swift

当我正在搜索然后切换 UItab 时,不会调用 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 不是在 UITableViewController 上调用的,而是在 UISearchController 上调用的。

所以我继承了 UISearchController 并覆盖了 viewWillDisappear 方法。在我的例子中,我只需要停用搜索 Controller 。

class SearchController: UISearchController {

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

关于ios - ViewWillDisappear 没有被调用 searchcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31657402/

相关文章:

iphone - 如何根据异步检索的数据更新 ViewController 的属性?

ios - 在没有动画效果的avplayerlayer上设置videogravity

ios - 垂直对齐 UITableViewCell 中的 UITextlabel

ios - 如何使用分钟数据过滤特定的一天时间作为 Swift 3 中多个索引中的单个索引存储到数组中

ios - 将大量数据从核心数据导出到json

ios - 当设备旋转改变时创建附加约束

ios - 使用 Xcode 6 仍然得到 "Unable to boot iOS Simulator"

ios - UISwitch 不应该以相反的方式工作吗?

objective-c - UIPrintInteractionController 在 iOS 13 测试版设备中无法正常工作

iphone - 滚动时基本 UITableView 的内存问题