Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从嵌入父视图控制器的子视图控制器中的UISearchViewController显示视图控制器?_Ios_Swift_Uisearchcontroller_Childviewcontroller - Fatal编程技术网

Ios 如何从嵌入父视图控制器的子视图控制器中的UISearchViewController显示视图控制器?

Ios 如何从嵌入父视图控制器的子视图控制器中的UISearchViewController显示视图控制器?,ios,swift,uisearchcontroller,childviewcontroller,Ios,Swift,Uisearchcontroller,Childviewcontroller,我有一个名为MainViewController的父视图控制器,和一个名为FilterViewController的子视图控制器,它占据了主控制器屏幕的一部分 在filterViewController中,我向tableview添加了一个UISearchController。但是当我点击搜索结果转到DetailViewController时 尝试呈现已呈现的内容 我在iPadiOS8模拟器上使用这个。 如果resultSearchController未处于活动状态,我可以单击该表并将其转到我显示

我有一个名为MainViewController的父视图控制器,和一个名为FilterViewController的子视图控制器,它占据了主控制器屏幕的一部分

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

尝试呈现已呈现的内容

我在iPadiOS8模拟器上使用这个。 如果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)
}
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
}
filtervewcontroller.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)
}
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
}

如果问题是视图控制器仍然显示resultSearchController,为什么不让resultSearchController显示新的vc

resultSearchController.presentViewController(detailVC, animated: false, completion: nil)
然后,如果需要从resultSearchController中退出,只需创建一个从UISearchController继承的新类