Search code examples
iosxcodeuitableviewuisearchdisplaycontrolleruitableviewsectionheader

Xcode 8: Adding UISearchBarController to UITableViewHeaderFooterView crash: nib must contain exactly one top level object


I am building my Section Header View in a XIB file:

enter image description here

It's a XIB file that consists of a UISearchBarController inside a UIView

I register it in viewDidLoad in a tableViewController:

tableView.register(UINib(nibName: "MessagesSH", bundle: nil), forHeaderFooterViewReuseIdentifier: "MessagesSH")

I call it in the viewForHeaderInSection:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

      // Below is the where the app crashes
      let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "MessagesSH") as? MessagesSH 

      return headerView
    }

When I run the app, it crashes:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (MessagesSH) - nib must contain exactly one top level object which must be a UITableViewHeaderFooterView instance'


Solution

  • In my case, I discovered that you cannot have any type of a controller (i.e. UIViewController, UISearchBarController, etc.) in a Xib file. I had to use a storyboard instead of a XIB.