IOS Swift 如何获取 View 1 到 View 2 上的 SearchBar 文本值

标签 ios swift swift3 uisearchbar

我有 2 个 Controller :Controller1 和 Controller2,用作 SubView 。 Controller1 附加了一个 UISearchBar ,OnClick Controller2 显示为带有 TableView 的 SubView 。当用户在搜索栏中输入内容时,我可以使用它来获取结果

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

最佳答案

试试这个:

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 之后的 Controller 2 中添加 var:

var textFromSearch = ""

关于IOS Swift 如何获取 View 1 到 View 2 上的 SearchBar 文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40598026/

相关文章:

ios - 在Swift 3中使用.droplast()删除空的小数

ios - 编写一个对一个项目可见而对其他项目隐藏的类方法?

ios - swift tableview 重新加载数据问题 - 自定义单元格中缺少元素

swift - ViewController segue 和登录

swift3 - 如何启用 "-fnative-double"?

ios - 我想在 iOS Swift 中摇动谷歌地图标记

android - 在 iOS 和 Android 之间同步应用程序数据

ios - 无法在 Swift 中显示 map 注释

ios - 尝试将 maximumValue 设置为 UISlider 时应用程序崩溃

ios - 执行 UIAlertController 时获取 nil