2017-08-17 103 views
0

我已經使用嵌入導航欄設置了搜索欄。 func searchBarSearchButtonClicked未被檢測到。我想這樣做是爲了當用戶點擊搜索欄時,會調用另一個函數。我已經拿出了一些與這個問題無關的無關代碼。可能是什麼問題?當用戶點擊搜索欄上searchBarSearchButtonClicked未被檢測到

class FirstViewController: UIViewController, UISearchBarDelegate { 

var resultSearchController: UISearchController? = nil      

    override func viewDidLoad() { 
    super.viewDidLoad() 

    // set up the search results table 
    let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable 

    resultSearchController = UISearchController(searchResultsController: locationSearchTable) 
    resultSearchController?.searchResultsUpdater = locationSearchTable 

    let searchBar = resultSearchController!.searchBar 
    searchBar.sizeToFit() 
    searchBar.placeholder = "Where would you like to go" 
    navigationItem.titleView = resultSearchController?.searchBar 
    searchBar.delegate = self 

    resultSearchController?.hidesNavigationBarDuringPresentation = false 
    resultSearchController?.dimsBackgroundDuringPresentation = true 
    definesPresentationContext = true         
    }        

    func searchBarSearchButtonClicked(_: UISearchBar) { 
    // closeMapType() 
    // self.mapType.transform = .identity 
    print("it worked") 
    }                  
} 
+0

代碼似乎點擊「搜索」按鈕你在設備上嘗試過很好嗎?並按下輸入,如果在模擬器的情況下 –

+0

是的,它沒有工作,打印功能沒有顯示 – zachenn

回答

0

以下函數將調用:

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { 
    return true 
} 

這將調用當我們在鍵盤

func searchBarSearchButtonClicked(_: UISearchBar) 
+0

好吧,它的工作!我也剛剛意識到,我試圖調用的其中一個功能是在另一個類中。我如何從其他課程調用函數?我需要使用擴展嗎?或者是它的這個FirstViewController類,我目前在 – zachenn

+0

內可能我想通了以「FloatingActionButton()beginTracking。(觸摸:UITouch,有:?的UIEvent)」你有什麼想法,我應該把什麼UITouch或的UIEvent? – zachenn