我的项目设置如下:
我有一个自定义标签导航控制器。在自定义选项卡导航控制器的顶部栏中是一个文本字段。此文本字段根据应用程序的4个主视图(4个选项卡按钮)更改。
我想要做的是将文本字段用作搜索栏,方法是将输入文本字段的内容传递给searchView但是,我无法将textfield.text从导航控制器传递到searchView。我在下面有一张图片,更清楚地说明了这一点。
searchView具有搜索功能。我想要做的就是在每次更改时将textfieled.text值传递给searchView
答案 0 :(得分:1)
你可以做那样的事情。 在你的FirstViewController
中 func textFieldDidChange(textField: UITextField){
let search_screen = SearchViewController()
search_screen.search_string = self.textField.text
self.navigationController?.pushViewController(search_screen, animated: true)
}
如果是故事板
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "Your_SearchViewController_identifier") {
let search_screen = segue.destinationViewController as!
SearchViewController
search_screen.search_string = self.textField.text
}
}
在你的SeacrchViewController' s viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.textField.text = self.search_string
}
答案 1 :(得分:1)
// global
let handleTextChangeNotification = "handleTextChangeNotification"
您的FirstViewController
override func viewDidLoad() {
super.viewDidLoad()
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
}
func textFieldDidChange(textField: UITextField){
NotificationCenter.default.post(name: NSNotification.Name(rawValue: handleTextChangeNotification), object: nil, userInfo: ["text":textField.text])
let search_screen = SearchViewController()
self.navigationController?.pushViewController(search_screen, animated: true)
}
SeacrchViewController的
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(SeacrchViewController.handleTextChange(_:)),
name: NSNotification.Name(rawValue: handleTextChangeNotification),
object: nil)
}
func handleTextChange(_ myNot: Notification) {
if let use = myNot.userInfo {
if let text = use["text"] {
// your search with 'text' value
}
}
}
}