0

我有两个表格视图,我通过选择分段控件在它们之间切换。我还有一个带有搜索栏的搜索控制器。当我选择搜索栏时,应该禁用分段控件,这样您就无法选择第二个表格视图。当我单击搜索栏中的取消按钮时,应该再次激活分段控件。但这不起作用,分段控件保持禁用状态。当我选择一个表格视图单元格并返回到此视图控制器时,分段控件将再次启用。但是我希望在按下取消按钮时启用它。我的代码如下所示:

   func deactivateSegment() {
    segmentedController.setEnabled(false, forSegmentAt: 1)
}

func activateSegment() {
    segmentedController.setEnabled(true, forSegmentAt: 1)
}


extension SegmentedTableController: UISearchResultsUpdating, UISearchBarDelegate {

func updateSearchResults(for searchController: UISearchController) {
    deactivateSegment()
    if searchController.searchBar.text != "" {
        filterContentForSearchText(searchText: searchController.searchBar.text!)
    } else {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "update_view"), object: rumList, userInfo: nil)
    }
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    KCFABManager.defaultInstance().hide()
    deactivateSegment()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    KCFABManager.defaultInstance().show()
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "update_view"), object: self.rumList, userInfo: nil)

   activateSegment()
}
}

我也试过了segmentedController.isEnabled = false (&true)segmentedController.isUserInteractionEnabled = false (&true) 但这也行不通。

我究竟做错了什么?

4

1 回答 1

0

删除deactivateSegment()委托方法中的updateSearchResults

func updateSearchResults(for searchController: UISearchController) {
   // deactivateSegment()
  if searchController.searchBar.text != "" {

否则喜欢

  func updateSearchResults(for searchController: UISearchController) {

    if searchController.searchBar.text != "" {
        filterContentForSearchText(searchText: searchController.searchBar.text!)
        activateSegment()
    } else {
        deactivateSegment()
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "update_view"), object: rumList, userInfo: nil)
    }
}
于 2017-03-01T10:31:58.830 回答