ios - searchController异常行为

标签 ios swift uisearchcontroller

class SearchMembers: UITableViewController, UISearchControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        navigationItem.title = NSLocalizedString("MEMBERS", comment: "")

            let search = UISearchController(searchResultsController: nil)
            self.definesPresentationContext = true
            search.searchResultsUpdater = self
            search.dimsBackgroundDuringPresentation = false
            navigationItem.searchController = search
            navigationItem.hidesSearchBarWhenScrolling = false

    }

当我从 tableview 单元格执行 segue 时,我可以看到搜索 Controller ,但当我从导航栏项目执行 segue 时,我只看到一个没有搜索栏的空间。然后,我回到上一个 View ,搜索 Controller 栏出现在那里。

如果我再次返回搜索 View ,它会恢复正常行为。

可能的错误?

enter image description here

应该是: enter image description here

编辑:

我意识到,如果我从 segue 设置中取消选择“Animates”选项,我在 searchController View 中不会出现空白区域,但当我返回主视图时我仍然会出现空白区域

最佳答案

检查您的 segue 是如何设置的。您是否有一个从 View Controller 连接到 View Controller 的 segue,或者它是从 tableview 连接到另一个 View Controller ,还是您有两个 segue?在这种情况下,你应该使用一个 segue,只是在不同的地方触发它

performSegue(withIdentifier: "yourSegueIdentifier")

看看我快速为你编好的这段代码: 您也可以从我的 github 上试用并与您的进行比较。在这两种情况下,搜索栏都会出现。 https://github.com/verebes1/SearchBarHelp

import UIKit

class MainViewController: UITableViewController {

    var items = ["One", "Two", "Three"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    //MARK: - Tableview methods
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = items[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showMembers", sender: self)
    }


    //MARK: - Segue setup
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        /* Put any preparation here if needed */
    }

    //MARK: Navbar Button action
    @IBAction func searchBarButtonTapped(_ sender: Any) {
        performSegue(withIdentifier: "showMembers", sender: self)
    }

}

和第二个viewController:

import UIKit

class SearchMembersController: UITableViewController, UISearchControllerDelegate {

    var members = ["First", "Second", "Third", "Fourth"]

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        navigationItem.title = NSLocalizedString("MEMBERS", comment: "")

        let search = UISearchController(searchResultsController: nil)
        self.definesPresentationContext = true
        //search.searchResultsUpdater = self
        search.dimsBackgroundDuringPresentation = false
        navigationItem.searchController = search
        navigationItem.hidesSearchBarWhenScrolling = false
    }


    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return members.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = members[indexPath.row]

        return cell
    }
}

Segue

Segue setup

从评论中引用您的代码:

leftButton.addTarget(self, action: #selector(Profile.searchMembers), for: .touchUpInside) self.settingsButton.customView = leftButton 

这应该调用 performSegue(withIdentifier:“profileToMembers”,发件人:self) 代替 Profile.searchMembers。可能这是错误的,你可以把它包装在一个 @objc 函数中,比如

@objc func segueToMembers(){ 
     performSegue(withIdentifier: "profileToMembers", sender: self) 
} 

然后调用它

leftButton.addTarget(self, action: #selector(segueToMembers), for: .touchUpInside) self.settingsButton.customView = leftButton 

关于ios - searchController异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51001190/

相关文章:

ios - 搜索 TableView

ios - IOS中Storyboard中如何添加GMSMapView

json - fatal error : unexpectedly found nil while unwrapping an Optional value, Swift

ios - dequeueReusableCellWithReuseIdentifier 不通过 init 或 initWithCoder 函数

java - Appium无法在模拟器中安装ipa文件

ios - 对不同 iphone 上的 ios 图像大小感到困惑

xcode - UISearchController 不更新

swift - 如何使 UITableViewController 符合 UISearchResultsUpdating 协议(protocol)?

ios - Swift SearchResultsController Segue - "Receiver ... has no segue with identifier ' testSeg ''"

iOS Swift 如何根据下拉列表中的选择显示动态用户界面