ios - 如何从嵌入在父 View Controller 的 subview Controller 中的 UISearchViewController 中呈现 View Controller ?

标签 ios swift uisearchcontroller childviewcontroller

我有一个名为 MainViewController 的父 View Controller 和一个名为 FilterViewController 的 subview Controller ,它占据了主 Controller 屏幕的一部分。

在 filterViewController 中,我向 tableview 添加了一个 UISearchController。但是当我点击搜索结果转到 DetailViewController

Attempt to present <MyApp.DetailViewController: 0x7f97ae057780> on <MyApp.FilterViewController: 0x7f97abd62b40> which is already presenting <UISearchController: 0x7f97abd85270>

我在 iPad iOS 8 模拟器上使用它。 如果 resultSearchController 未处于事件状态,我可以单击表格,它会转到我显示的任何内容,但不知何故这个 uisearchcontroller 不允许我这样做,有什么解决方法?

代码: MainViewController.swift

   func addFilterViewController(){
    filterViewController = self.storyboard?.instantiateViewControllerWithIdentifier("filterviewcontroller") as! FilterViewController
    filterViewController.mainViewController = self
    self.addChildViewController(filterViewController)
    filterViewController.view.frame = CGRectMake(self.view.frame.width*2/3, 0, self.view.frame.width/3, self.view.frame.height)
    self.view.addSubview(filterViewController.view)
    filterViewController.didMoveToParentViewController(self)
}

FilterViewController.swift

func setUpSearchBar() {
    resultSearchController = UISearchController(searchResultsController: nil)
    resultSearchController.searchResultsUpdater = self
    resultSearchController.dimsBackgroundDuringPresentation = false

    let searchBar = self.resultSearchController.searchBar
    searchBar.sizeToFit()
    searchBar.backgroundImage = UIImage()
    searchBar.barTintColor = UIColor.clearColor()
    searchBar.backgroundColor = UIColor.lightBlue()
    searchBar.placeholder = "Type to search"
    filterTable.tableHeaderView = searchBar
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if resultSearchController.active {
        println("search table select")
        let detailVC = self.storyboard?.instantiateViewControllerWithIdentifier("detailViewController") as! DetailViewController
        detailVC.card = filteredCards[indexPath.row]

        // this has same problem if I pass in the MainViewController and do mainViewController.presentViewController...
        self.presentViewController(detailVC, animated: false, completion: nil)

    } else {
        // other code
    }

    filterTable.deselectRowAtIndexPath(indexPath, animated: true)
}

func updateSearchResultsForSearchController(searchController: UISearchController) {
    self.filteredCards = self.unorderedCards.filter{
        (card: Card) -> Bool in
        let name = card.firstName + card.lastName
        let stringMatch = tributeName.lowercaseString.rangeOfString(searchController.searchBar.text.lowercaseString)
        return (stringMatch != nil)
    }
    filterTable.reloadData()

}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  if resultSearchController.active {
       return filteredCards.count
   } else {
       return unOrderedCards.count
   }}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("filtercell", forIndexPath: indexPath) as! FilterCell
    cell.backgroundColor = UIColor.lightBlue()
    if resultSearchController.active {

        let card = filteredCards[indexPath.row]
        cell.name.text = card.firstName

    } else {
        let card = unOrderedCards[indexPath.row]
        cell.name.text = card.firstName
    }
    return cell
}

最佳答案

如果问题是 View Controller 尚未呈现 resultSearchController,为什么不让 resultSearchController 呈现新的 vc?

resultSearchController.presentViewController(detailVC, animated: false, completion: nil)

然后,如果您需要从 resultSearchController 中关闭,只需创建一个继承自 UISearchController 的新类即可。

关于ios - 如何从嵌入在父 View Controller 的 subview Controller 中的 UISearchViewController 中呈现 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32315855/

相关文章:

ios - HeaderView 按钮以编程方式转到另一个 View Controller

iOS UISearchController TableView

ios - 这个 iOS 错误是什么?它来自哪里?

ios - 当上一个 View 来自导航 Controller 时如何保留 TabBar 界面

ios - 检测运行目标是App Extension还是iOS App

swift - 在 cocoa 后台连续运行代码

swift - SpriteKit SKSprite节点

ios - 制作多行,使用 React-Native 扩展 TextInput

ios - searchController异常行为

ios - UISearchController 仅在单击搜索按钮时更新