0
我有2个控制器:Controller1和Controller2作为一个子视图。 Controller1具有附加的UISearchBar,OnClick Controller2显示为带有TableView的SubView。由于在搜索栏,用户类型,我可以使用这个IOS Swift如何获得SearchBar文本值查看1到查看2
// 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值并将其传递到控制器2得到的结果,我怎么能去这样做? Controller2执行HTTP Post请求,并使用Controller1中的SearchText的值。这是在控制器2
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
}
}
这是在控制器1和上的UISearchBar的水龙头,这是我的代码怎么弄控制器2作为一个子视图
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
}
视图控制器的层次结构如何?两者是否有不同的导航控制器,或相同的? – Sethmr
他们只是2个不同的ViewControllers没有导航控制器 – user1591668
两个不同的视图控制器在哪里?他们必须处于某种或某种形式的层次结构中,以便您同时需要访问他们两个。 – Sethmr