user1591668 user1591668 - 20 days ago 9
Swift Question

IOS Swift How can I get SearchBar Text value on View 1 to View 2

I have 2 Controllers: Controller1 and Controller2 which serves as a SubView . Controller1 has a UISearchBar attached to it and OnClick Controller2 shows up as a SubView with a TableView . As a user types in the SearchBar I can get the results using this

// 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

}
}


Now my biggest issue is getting the value that I get in searchText and passing it over to Controller2, How can I go about doing this ? Controller2 does an HTTP Post request and will use the value of the SearchText in Controller1 . This is the code in 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
}



}


This is in Controller1 and on tap of the UISearchBar this is how I get Controller2 as a 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
}

Answer

try this:

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
    }

and in your Controller 2 just after the IBOutlet add var:

var textFromSearch = ""
Comments