警告:尝试显示其视图不在窗口层次结构中!在UISearchController中

时间:2015-06-16 18:40:13

标签: ios xcode swift segue uisearchcontroller

我在UITableViewController和“UISearchController”时出错。我做的不同。我在viewDidLoad中写了self.definesPresentationContext = true,但是我得到了同样的错误。

警告:尝试显示其视图不在窗口层次结构中!

请帮忙。

我的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返回的返回按钮

1 个答案:

答案 0 :(得分:0)

我修复了我的代码并且有效。

 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

            }
        }
    }
}