2017-08-17 136 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