通过搜索栏创建到另一个视图控制器的 segue?
Create a segue to another view controller through search bar?
如何通过搜索栏创建到另一个视图控制器的转接?
结果搜索栏的字符串值以编程方式转至 newViewController 中的新字符串变量。我该怎么做?
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
// 在这里我试图捕捉用户输入
userInput = "http://api.giphy.com/v1/gifs/search?" + "q=" + searchBar.text! + "&api_key=dc6zaTOxFJmzC"
performSegue(withIdentifier: "searchView", sender: self)
}
//我的转场
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
如果 segue .identifier == "searchView" {
让 DestViewController = segue.destination 作为!搜索结果控制器
DestViewController.userInputRequest = 用户输入
}
//我的新视图控制器
class SearchResultController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchBarDelegate {
变种用户输入请求:字符串=“”
让 userRequestArray = [Image]()
重写 func viewDidLoad() {
}
首先,确保 searchBar.delegate
已连接到 viewController。
你应该实施 searchBarSearchButtonClicked(_:) method from UISearchBarDelegate:
Tells the delegate that the search button was tapped.
在你的例子中,当用户点击键盘上的 "Search" 按钮时,它会被调用。
因此,您应该执行以下操作:
// don't forget to add 'UISearchBarDelegate'
class ViewController: UIViewController, UISearchBarDelegate {
//...
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let text = searchBar.text {
// here is text from the search bar
print(text)
userInput = text
// now you can call 'performSegue'
performSegue(withIdentifier: "searchView", sender: self)
}
}
}
编辑:
如果你不使用故事板(和 segues),代码应该是这样的:
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let text = searchBar.text {
// here is text from the search bar
print(text)
let searchResultController: SearchResultController = SearchResultController()
searchResultController.userInputRequest = text
navigationController?.pushViewController(searchResultController, animated: true)
}
}
希望对您有所帮助。
如何通过搜索栏创建到另一个视图控制器的转接? 结果搜索栏的字符串值以编程方式转至 newViewController 中的新字符串变量。我该怎么做?
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { // 在这里我试图捕捉用户输入 userInput = "http://api.giphy.com/v1/gifs/search?" + "q=" + searchBar.text! + "&api_key=dc6zaTOxFJmzC" performSegue(withIdentifier: "searchView", sender: self) }
//我的转场 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 如果 segue .identifier == "searchView" { 让 DestViewController = segue.destination 作为!搜索结果控制器 DestViewController.userInputRequest = 用户输入 }
//我的新视图控制器 class SearchResultController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchBarDelegate { 变种用户输入请求:字符串=“” 让 userRequestArray = [Image]() 重写 func viewDidLoad() { }
首先,确保 searchBar.delegate
已连接到 viewController。
你应该实施 searchBarSearchButtonClicked(_:) method from UISearchBarDelegate:
Tells the delegate that the search button was tapped.
在你的例子中,当用户点击键盘上的 "Search" 按钮时,它会被调用。
因此,您应该执行以下操作:
// don't forget to add 'UISearchBarDelegate'
class ViewController: UIViewController, UISearchBarDelegate {
//...
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let text = searchBar.text {
// here is text from the search bar
print(text)
userInput = text
// now you can call 'performSegue'
performSegue(withIdentifier: "searchView", sender: self)
}
}
}
编辑:
如果你不使用故事板(和 segues),代码应该是这样的:
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let text = searchBar.text {
// here is text from the search bar
print(text)
let searchResultController: SearchResultController = SearchResultController()
searchResultController.userInputRequest = text
navigationController?.pushViewController(searchResultController, animated: true)
}
}
希望对您有所帮助。