ViewWillDisappear没有被称为searchcontroller

时间:2015-07-27 15:39:19

标签: ios objective-c xcode swift

当我处于搜索中间然后切换UItabs时,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

}

3 个答案:

答案 0 :(得分:11)

有同样的问题。 viewWillDisappear未调用UITableViewController,但会在UISearchController中调用{。}}。

所以我将UISearchController子类化并覆盖了viewWillDisappear方法。在我的情况下,我只需要停用搜索控制器。

class SearchController: UISearchController {

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

答案 1 :(得分: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)
    }
}

答案 2 :(得分:-4)

我认为它是xcode的问题。尝试关闭它并再次重新打开项目并尝试再次运行