2017-03-01 43 views
0

我有兩個表視圖,我通過選擇分段控件在它們之間切換。我也有一個搜索控制器與搜索欄。當我選擇搜索欄時,應該禁用分段控件,以便您不能選擇第二個表視圖。當我點擊搜索欄中的取消按鈕時,應該再次激活分段控件。但這不起作用,分段控制保持禁用狀態。 當我選擇一個表格視圖單元格並返回到此視圖控制器時,再次啓用分段控件。但是我想在按下取消按鈕時啓用它。 我的代碼如下:Swift 3 - 禁用並啓用分段控件

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) 嘗試過,但也不能正常工作。

我在做什麼錯?

+0

刪除這個'''中'updateSearchResults –

+0

是啊,這做到了,deactivateSegment()謝謝!我從updateSearchResults中刪除了停用,現在它按我的意願工作。 – MightyAlienDwarf

+0

歡迎....... –

回答

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) 
    } 
}