gpt4 book ai didi

ios - Swift:进入 UITableView 搜索

转载 作者:行者123 更新时间:2023-11-29 01:48:12 25 4
gpt4 key购买 nike

我想继续 UITableView 搜索。我已经实现并运行了 uitableview 和搜索栏。现在我想添加一个按钮,单击该按钮时,可以在拔出键盘的情况下打开搜索 View 。我该如何实现?

挑战:如何将这个按钮放在单独的 View Controller 上?

我已经查找了进入搜索栏的方法,但我发现的只是从搜索中退出的方法。

我的代码:

/*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。

代码示例:

初始 View Controller

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
}
}

}

下一个 View Controller

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()
}
}

}

关于ios - Swift:进入 UITableView 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31684636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com