IOS Swift如何在View 1到View 2上获取SearchBar Text值

时间:2016-11-14 21:21:56

标签: ios swift swift3 uisearchbar

我有2个控制器:Controller1和Controller2作为SubView。 Controller1附加了一个UISearchBar,OnClick Controller2显示为带有TableView的SubView。当用户在SearchBar中输入时,我可以使用此

获取结果
 // Controller1
   @IBOutlet weak var mySearch: UISearchBar!

override func viewDidLoad() {
    super.viewDidLoad()
    mySearch.delegate = self
    // Do any additional setup after loading the view.
}
 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
        if let text = searchBar.text {
            let search = text.trimmingCharacters(in: .whitespaces)
            _ = search

        }
    }

现在我最大的问题是获得 searchText 带来的价值并将其传递给Controller2,我该如何做到这一点? Controller2执行HTTP Post请求,并将使用Controller1中的SearchText值。这是Controller2中的代码

 class Controller2:  UIViewController,UITableViewDataSource {
@IBOutlet weak var TableSource: UITableView!



override func viewDidLoad() {
    super.viewDidLoad()

    TableSource.dataSource = self

   // I would like to get value of SearchText here so that I can
   // send it as a parameter in my HttpPost request

    session.dataTask(with:request, completionHandler: {(data, response, error) in
        if error != nil {

        } else {
            do {

                // get Json Data

            } catch let error as NSError {
                print(error)
            }
            print(self.locations)
        }

    }).resume()



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
    return locations.count
}





func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Registration_Search", for: indexPath)

    return cell
}



 }

这是在Controller1中,在UISearchBar上,这就是我将Controller2作为SubView的方式

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
           Location_Search.showsCancelButton = true

        let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Controller2") as! Controller2
        self.addChildViewController(Popup)
        Popup.view.frame = self.Controller2.frame
        self.view.addSubview(Popup.view)
        Popup.didMove(toParentViewController: self)
        return true
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
           Location_Search.showsCancelButton = true

        let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Controller2") as! Controller2
        self.addChildViewController(Popup)
        Popup.view.frame = self.Controller2.frame
        Popup.textFromSearch = mySearch.text
        self.view.addSubview(Popup.view)
        Popup.didMove(toParentViewController: self)
        return true
    }

并在IBOutlet添加var:

之后的 Controller 2
var textFromSearch = ""