简体   繁体   English

如何从嵌入在父视图控制器的子视图控制器中的 UISearchViewController 呈现视图控制器?

[英]How do I present a view controller from UISearchViewController embedded in a child view controller of a parent view controller?

I have a parent view controller called MainViewController, and a child view controller called FilterViewController that takes partial of the main controller screen.我有一个名为 MainViewController 的父视图控制器和一个名为 FilterViewController 的子视图控制器,它占用了主控制器屏幕的一部分。

Inside the filterViewController I added a UISearchController to the tableview.在 filterViewController 中,我向 tableview 添加了一个 UISearchController。 But When I click on the search result to go to DetailViewController但是当我点击搜索结果去 DetailViewController

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

I am using this on iPad iOS 8 simulator.我在 iPad iOS 8 模拟器上使用它。 If the resultSearchController is not active I can click the table and it goes to whatever I present, but somehow this uisearchcontroller won't allow me, what is a workaround to this?如果 resultSearchController 未处于活动状态,我可以单击表格,它会转到我呈现的任何内容,但不知何故这个 uisearchcontroller 不允许我,有什么解决方法?

Code: MainViewController.swift代码: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 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
}

If the problem is the view controller is yet presenting resultSearchController, why not let resultSearchController present the new vc?如果问题是视图控制器还没有呈现 resultSearchController,为什么不让 resultSearchController 呈现新的 vc?

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

Then, if you need to make the dismiss from resultSearchController, just create a new class inheriting from UISearchController.然后,如果您需要从 resultSearchController 中关闭,只需创建一个继承自 UISearchController 的新类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加一个已经有父 controller 的视图 controller 作为另一个视图 controller 的子视图 - How do I add a view controller that already has a parent controller as a child of another view controller 如何从子视图控制器访问父视图控制器的视图? - how can i access a parent view controller's view from a child view controller? 父视图控制器中的子视图控制器 - Child View Controller within Parent View Controller 如何将数据从父视图控制器传递到子视图控制器? - How can I passing data from parent view controller to child view controller? 从嵌入式视图控制器执行父segue - Perform a parent segue from the embedded view controller 将数据从父视图控制器传递到子视图控制器Swift 4 - Passing Data From Parent View Controller to Child View Controller Swift 4 如何以模态方式呈现嵌入导航控制器中的视图控制器? - How to present view controller embedded in navigation controller modally? 如何从嵌入式容器视图的视图引用视图控制器的标题? - How do I refer to the title of a view controller from an embedded container view's view? 如何从分离的视图控制器中呈现视图控制器? - How to present a view controller from a detached view controller? UISearchViewController不允许在取消分配时尝试加载视图控制器的视图 - Attempting to load the view of a view controller while it is deallocating is not allowed for UISearchViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM