[英]Swift: Segue into a UITableView Search
我想進入一個UITableView搜索。 我已經實現並工作了uitableview和搜索欄。 現在,我想添加一個按鈕,當單擊該按鈕時,將在不使用鍵盤的情況下打開搜索視圖。 我該如何實施?
挑戰:如何將此按鈕放在單獨的視圖控制器上?
我已經找到了可以搜索到搜索欄的方法,但是我發現的所有方法都是可以從搜索中選擇搜索的方法。
我的代碼:
/*Search*/
var searched: Bool = false
@IBOutlet weak var searchBarReal: UISearchBar!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
getSortedSectionList()
tableView.reloadData()
if searched {
searchBarReal.becomeFirstResponder()
searched = false
}
}
override func viewDidLoad() {
super.viewDidLoad()
let nav = self.navigationController?.navigationBar
nav?.backgroundColor = UIColor.whiteColor()
nav?.tintColor = UIColor.whiteColor()
setUpIcons()
searchBarReal.delegate = self
}
您可以告訴您的searchBar成為FirstResponder。
代碼示例:
初始視圖控制器
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func bttnTouched(sender: AnyObject) {
performSegueWithIdentifier("next", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "next" {
let nextVc = segue.destinationViewController as! NextViewController
nextVc.shouldSearchBarRespond = true
}
}
}
下一視圖控制器
import UIKit
class NextViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
var shouldSearchBarRespond: Bool?
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
if shouldSearchBarRespond == true {
searchBar.becomeFirstResponder()
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.