警告:尝试呈现不在窗口层次结构中的视图!在 UISearchController

Posted

技术标签:

【中文标题】警告:尝试呈现不在窗口层次结构中的视图!在 UISearchController【英文标题】:Warning: Attempt to present on whose view is not in the window hierarchy! in UISearchController 【发布时间】:2015-06-16 18:40:13 【问题描述】:

UITableViewController 和“UISearchController”出现错误。我做的不同。我将self.definesPresentationContext = true 写入viewDidLoad,但我得到了同样的错误。

警告:尝试呈现不在窗口层次结构中的视图!

请帮忙。

我的 viewDidLoad 代码

    override func viewDidLoad() 
    super.viewDidLoad()

    self.navigationItem.leftBarButtonItem = self.editButtonItem()

    fetchedResultsController = NSFetchedResultsController(fetchRequest: allEmployeesFetchRequest(), managedObjectContext: managedObjectContext!, sectionNameKeyPath: "mynote", cacheName: "mynote") // both mynote
    fetchedResultsController?.delegate = self
    fetchedResultsController?.performFetch(nil)

    self.searchController = (
        var controllerS = UISearchController(searchResultsController: nil)
        controllerS.delegate = self
        controllerS.searchBar.delegate = self
        controllerS.searchResultsUpdater = self
        controllerS.searchBar.sizeToFit()
        controllerS.dimsBackgroundDuringPresentation = false
        controllerS.hidesNavigationBarDuringPresentation = false
     //   self.definesPresentationContext = true
        self.tableView.tableHeaderView = controllerS.searchBar
        return controllerS
    )()

我的 prepareForSegue 代码

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) 
    if segue.identifier == "showDetail" 
        if let indexPath = self.tableView.indexPathForSelectedRow() 
            if searchPredicate == nil 
            let object = self.fetchedResultsController!.objectAtIndexPath(indexPath) as! NSManagedObject
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController //DetailViewController
                controller.detailItem = object
             else 
                let object = filteredObjects![indexPath.row] as Note
                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = object
            
        
        self.searchController.active = false
    

更新 如果我制作了self.definesPresentationContext = true,那么我需要触摸两个一次返回按钮,这些按钮将在UITableViewController 上从SecondViewController 返回

【问题讨论】:

if let indexPath == self.tableView.indexPathForSelectedRow() 不,它得到了一个错误 Brocked block 如果你在 viewDidAppear 中做同样的事情,它会起作用吗? 它可以工作,但是我需要在返回按钮上触摸两个从 SecondViewController 中返回的 UITableViewController。为什么? “触摸两次”是什么意思? 【参考方案1】:

我修复了我的代码,它可以工作了。

 var searchController: UISearchController!

func updateSearchResultsForSearchController(searchController: UISearchController) 
    self.dataSearchResults?.removeAll(keepCapacity: false)
    var searchText = searchController.searchBar.text
    var query: PFQuery = PFQuery(className: "NewsNow")
    if searchController.active == true  
        query.whereKey("nameNews", matchesRegex: searchText, modifiers: "i")
        self.tableView.reloadData() // 4 win!!!
    
    query.findObjectsInBackgroundWithBlock  (results:[AnyObject]?, error: NSError?) -> Void in
        self.dataSearchResults = results as? [PFObject]
        self.tableView.reloadData()
    
     println(searchText)


func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) 
    updateSearchResultsForSearchController(searchController)


func didDismissSearchController(searchController: UISearchController) 
    self.dataSearchResults = nil //nil
    self.tableView.reloadData()


    override func viewDidLoad() 
    super.viewDidLoad()
    var object: PFObject?

    var query = PFQuery(className: "NewsNow")
    query.getFirstObjectInBackgroundWithBlock  (object, error) -> Void in
        if error == nil 
            var imageFile: PFFile = object!.objectForKey("imageNews") as! PFFile
        
    

    self.searchController = (
        var controllerSearch = UISearchController(searchResultsController: nil)
        controllerSearch.delegate = self
        controllerSearch.searchBar.delegate = self
        controllerSearch.searchResultsUpdater = self
        controllerSearch.dimsBackgroundDuringPresentation = false
        controllerSearch.hidesNavigationBarDuringPresentation = true
        controllerSearch.hidesBottomBarWhenPushed = true
        controllerSearch.searchBar.sizeToFit()
        controllerSearch.definesPresentationContext = false  //default false
        self.tableView.tableHeaderView = controllerSearch.searchBar
        return controllerSearch
    )()


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    var imagePass: UIImage! // image
    var nameTextPass: String! // name
    var allTextPass: String! // all text in string
    var textURLPass: String! // url of news
    var dateTextPass: String! // data of news

    if segue.identifier == "showParse" || segue.identifier == "showDetailParse" 
        if let indexPath = self.tableView.indexPathForSelectedRow() 
            if let detailController = (segue.destinationViewController as! UINavigationController).topViewController as? DetailViewController 

                var currentCell = tableView.cellForRowAtIndexPath(indexPath) as! ParseTableViewCell

                imagePass = currentCell.imageViewCell.image
                detailController.image = imagePass

                nameTextPass = currentCell.labelNameText.text
                detailController.textName = nameTextPass

                allTextPass = currentCell.labelText
                detailController.textOfText = allTextPass

                // link on the news
                textURLPass = currentCell.labelURLNews
                detailController.textOfURL = textURLPass

                dateTextPass = currentCell.labelDataText.text 
                detailController.dateNewsDetail = dateTextPass

            
        
    

【讨论】:

以上是关于警告:尝试呈现不在窗口层次结构中的视图!在 UISearchController的主要内容,如果未能解决你的问题,请参考以下文章

警告:尝试在视图不在窗口层次结构中的 **.ViewController 上呈现 ZMNavigationController

警告“尝试呈现视图不在窗口层次结构中的 ViewController - Objective C

警告:尝试在其视图不在窗口层次结构中的 ViewController 上呈现 ViewController (w/UIAlertController)

如何解决此错误“警告:尝试呈现其视图不在窗口层次结构中”?

Xamarin.Forms 警告:尝试使用 iOS 图像/手势识别器在其视图不在窗口层次结构中的 * 上呈现 *

尝试呈现其视图不在窗口层次结构中的 UIAlertController (Swift 3/Xcode 8)