简体   繁体   English

iOS Swift如何将搜索功能包含在另一个功能中

[英]IOS Swift how can I include the search function inside another function

I have 2 Controllers and one is a SubView. 我有2个控制器,一个是SubView。 The MainController has access to the SearchBar property and the SubViewController does not . MainController可以访问SearchBar属性,而SubViewController不能。 There is a SearchBar function in the Main Controller that I would like to invoke in the SubView Controller it is this function 我想在SubView Controller中调用主控制器中的SearchBar函数,此函数

      // MainController
      func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        // Stop doing the search stuff
        // and clear the text in the search bar
        searchBar.text = ""
        // Hide the cancel button
        searchBar.showsCancelButton = false
        searchBar.endEditing(true)
        Popup.Close_View()
      }

What I am trying to do is wrap that function above in another function like this 我想做的是将该函数包装在另一个这样的函数中

  func Selected_Location() {
        searchBarCancelButtonClicked(SearchBar)
    }

so that in my SubView Controller I can do 这样我就可以在SubView Controller中

// SubView Controller
   let Select_Close = MainController()

   Select_Close.Selected_Location()

I am new to Swift and as stated before how can I get the searchBarCancelButtonClicked function and call it inside another function with ? 我是Swift的新手,如前所述,如何获取searchBarCancelButtonClicked函数并在另一个函数中调用它? I have tried doing it in several ways such as 我已经尝试过几种方法,例如

  func Selected_Location() {
        searchBarCancelButtonClicked(_ searchBar: UISearchBar)
    }

    func Selected_Location() {
        searchBarCancelButtonClicked(searchBar)
    }

Again my SubView Controller does not have access to a UISearchBar so I am trying to call the SearchBar close function from within the SubView. 同样,我的SubView Controller无法访问UISearchBar,因此我试图从SubView内调用SearchBar关闭函数。

In Subview, keep weak reference of MainViewController, and then call one method of mainviewcontroller searchBarCancel 在Subview中,保持对MainViewController的弱引用,然后调用mainviewcontroller的一种方法searchBarCancel

class MainViewController:UIViewController {
    var subViewController: SubViewController
    func searchBarCancel(){
        searchBarCancelButtonClicked(self.searchBar)
    }
    func func2(){
        subViewController.mainController = self
    }
}

class SubViewController:UIView {
    weak var mainController: MainViewController?
    func Selected_Location() {
        mainController!.searchBarCancel()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM