我想将转换为 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
}
答案 0 :(得分:1)
您可以告诉您的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()
}
}
}