2016-08-02 49 views
2

我遇到搜索欄問題。我需要在右下角用搜索按鈕創建一個表格視圖,當我點擊它時,它應該顯示搜索欄。用動作顯示搜索欄(酒吧項目)

我的代碼是在這裏:

// Search controller 
searchController = ({ 
    let controller = UISearchController(searchResultsController: nil) 
    controller.delegate = self 
    controller.searchBar.delegate = self 
    controller.searchResultsUpdater = self 
    controller.dimsBackgroundDuringPresentation = false   
    controller.hidesNavigationBarDuringPresentation = true 
    controller.searchBar.sizeToFit() 
    return controller 
})() 

這裏是動作:

// Search action 
@IBAction func search(sender: UIBarButtonItem) { 
    print("Open search") 
    searchController.active = true 
    if searchController.searchBar.isFirstResponder() == false { 
     searchController.searchBar.becomeFirstResponder() 
    } 
} 

當我按一下按鈕,沒有任何反應(只打印在控制檯文本),而我要的是如下圖所示:

Show search

回答

8

類別需要以符合UISearchBarDelegate,我不確定你是否已經完成了。另外,還要確保你目前的搜索視圖

從蘋果公司的文檔:

的UISearchBarDelegate協議定義你實現做一個UISearchBar控制功能的可選方法。 UISearchBar對象爲欄上的搜索字段提供了用戶界面,但應用程序有責任在輕按按鈕時執行這些操作。當文本輸入到文本字段中時,代表至少需要執行實際的搜索。

下面是從我的應用程序的樣本

@IBAction func searchAction(sender: UIBarButtonItem) { 
    // Create the search controller and specify that it should present its results in this same view   
    searchController = UISearchController(searchResultsController: nil) 

    // Set any properties (in this case, don't hide the nav bar and don't show the emoji keyboard option) 
    searchController.hidesNavigationBarDuringPresentation = false 
    searchController.searchBar.keyboardType = UIKeyboardType.ASCIICapable 

    // Make this class the delegate and present the search 
    self.searchController.searchBar.delegate = self 
    presentViewController(searchController, animated: true, completion: nil) 
}