2015-08-31 6 views
2

У меня есть родительский контроллер представления, называемый MainViewController, и контроллер дочернего представления, называемый FilterViewController, который частично обрабатывает экран главного контроллера.Как представить контроллер представления из UISearchViewController, встроенный в контроллер дочернего представления родительского контроллера?

Внутри фильтраViewController я добавил UISearchController в tableview. Но когда я нажимаю на результат поиска, чтобы перейти к 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 
} 

ответ

1

Если проблема заключается в контроллер представления еще представления resultSearchController, почему бы не позволить resultSearchController представить новый ВК?

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

Затем, если вам нужно сделать уволить из resultSearchController, просто создать новый класс, унаследованный от UISearchController.

+0

omg вы спасли мою задницу ... спасибо! –