bnassler
bnassler

Reputation: 651

Programmatic UINavigationController and segue away

In my code I need to go through a navigation hierarchy, which is why I have a UIViewController in a UINavigationController.

If the user tabs on a cell and there is a next level, I create the same UIViewController again and push it on the UINavigationController. This works fine.

But when I reach the end of the hierarchy and try to performSegueWithIdentifier, to go to a different controller to see the details, the app crashes and says

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
 reason: 'Receiver (<myproject.MenuController: 0x7fb2e0ea9ef0>)
has no segue with identifier 'showDetail''

However I checked InterfaceBuilder, all fine. Interestingly, If I don't push a next level of navigation and perform the segue to the new controller directly, all works well.

I also tried to push the new controller, like I'm doing with the navigation, but then it tries to access the new controllers delegates which strangely are nil and crashes.

Somebody know how to do this?

Complete code (inside MenuContoller)

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
//self.performSegueWithIdentifier("showDetail", sender: self)
let clickedCell = self.tableViewItems[indexPath.row]
print("Selected \(clickedCell.itemID)")
// Check if subview has children, then we push a deeper level on the navigation controller
if (menuItems.filter{$0.parentID == clickedCell.itemID}.count > 0) {
  let subNavigationController = MenuController()
  // Set the currentNavigationItemID for the new view controller
  subNavigationController.currentNavigationItemID = clickedCell.itemID
  subNavigationController.title = clickedCell.itemDisplayName
  self.navigationController?.pushViewController(subNavigationController, animated: true)
} else {
  // Load new Controller to show details
  self.performSegueWithIdentifier("showDetail", sender: self)
//      let subNavigationController = ProductListViewController()
//      subNavigationController.currentCategoryID = clickedCell.itemID
//      self.navigationController?.pushViewController(subNavigationController, animated: true)
}

Upvotes: 0

Views: 176

Answers (1)

vacawama
vacawama

Reputation: 154711

The problem is that your MenuController has not been created from the Storyboard, so it has no segues attached to it.

You need to ask the Storyboard to create the MenuController, so instead of:

let subNavigationController = MenuController()

you need:

let subNavigationController = self.storyboard?.instantiateViewControllerWithIdentifier("menuControllerID")

For this to work, the storyboard ID must be set in the Identity Inspector to "menuControllerID" for MenuController.

Upvotes: 2

Related Questions