Fix: ‘doesn’t contain a view controller with identifier’ in Xcode

Fix: 'doesn't contain a view controller with identifier' in Xcode

Written by

in

Quick answer: This error means no view controller in that storyboard has the Storyboard ID you passed to instantiateViewController(withIdentifier:). Open the view controller in Interface Builder, set its Storyboard ID in the Identity inspector, and make sure you instantiate from the correct storyboard.

You call instantiateViewController(withIdentifier:) and the app crashes with Storyboard (<UIStoryboard>) doesn't contain a view controller with identifier 'drivingDetails', even though you think you set the identifier. This is almost always a Storyboard ID configuration problem, not a code problem.

Why this happens

instantiateViewController(withIdentifier:) looks for a view controller whose Storyboard ID matches the string you pass. It does not look at segue identifiers, class names, or Restoration IDs.

In Interface Builder there are two different identifiers that look similar. The segue identifier is set on the connection arrow between two scenes. The Storyboard ID is set on the view controller itself in the Identity inspector. If you set the segue identifier to drivingDetails but leave the Storyboard ID empty, the storyboard still has no view controller with that identifier and the call throws.

A second common cause is instantiating from the wrong storyboard object. self.storyboard is the storyboard that created self. If your drivingDetails scene lives in Main.storyboard but self was loaded from Settings.storyboard, or from a different storyboard file you created, self.storyboard will not contain it. The same happens when you create a UIStoryboard(name: "Main", bundle: nil) but the scene is actually in another file.

Case sensitivity and build cache also matter. drivingDetails and DrivingDetails are different strings. And if you just added the Storyboard ID, Xcode may still be running an old compiled storyboard until you clean and rebuild.

How to fix it

1. Set the Storyboard ID on the view controller, not the segue

This is the fix in almost every case.

  1. Open your .storyboard in Xcode.
  2. Click the view controller you want to instantiate. Click the yellow view controller icon in the scene dock or the view controller bar at the top of the scene to make sure you selected the view controller itself, not its view or a segue.
  3. Open the Identity inspector (View > Inspectors > Show Identity Inspector, or Option+Command+3).
  4. Find the Identity section. Set Storyboard ID to drivingDetails. Leave Class as your custom class if you have one, for example DrivingDetailsViewController.
  5. Check Use Storyboard ID if you want the Restoration ID to mirror it, but the required field for instantiateViewController(withIdentifier:) is Storyboard ID.

Do not set this on the segue. To verify, select the arrow between scenes and check the Attributes inspector. That identifier is for performSegue(withIdentifier:sender:) and prepare(for:sender:), not for instantiation.

Your code should then match exactly:

import UIKit

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard let controller = storyboard?.instantiateViewController(withIdentifier: "drivingDetails") as? DrivingDetailsViewController else {
        fatalError("Storyboard ID 'drivingDetails' not found")
    }
    controller.title = dao.libraryItem(at: indexPath.row).value(forKey: "name") as? String
    navigationController?.pushViewController(controller, animated: true)
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"drivingDetails"];
    controller.title = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"name"];
    [self.navigationController pushViewController:controller animated:YES];
}

If you see no “Identifier” field, you are looking in the wrong inspector. In modern Xcode the field is labeled Storyboard ID in the Identity inspector. Older tutorials called it “Identifier”.

2. Make sure you are instantiating from the correct storyboard

If Step 1 is correct and it still crashes, you are asking the wrong storyboard.

Check where the scene lives. If you have multiple storyboards, instantiate explicitly:

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller = mainStoryboard.instantiateViewController(withIdentifier: "drivingDetails")

Common mistakes:

  • Using self.storyboard when self came from a different storyboard. self.storyboard can be nil if the view controller was created in code or from a XIB.
  • Typo in the storyboard file name. UIStoryboard(name: "Main", bundle: nil) must match the filename without the .storyboard extension and is case-sensitive.
  • The scene is in a storyboard that is not included in your current target. Select the .storyboard file in the Project navigator, open the File inspector (Option+Command+1), and verify Target Membership is checked for your app target.

To debug, log which storyboard you are using:

print("storyboard =", storyboard as Any)
print("storyboard name =", storyboard?.value(forKey: "name") as Any)

If storyboard is nil, create the storyboard explicitly with UIStoryboard(name:bundle:) instead of using self.storyboard.

3. If the view controller is embedded in a navigation controller, instantiate the navigation controller

If your drivingDetails scene is embedded in a UINavigationController in the storyboard, the Storyboard ID must be on the navigation controller scene if you want to present the whole stack.

For example, if you embedded DrivingDetailsViewController in a navigation controller and set the Storyboard ID lastMinuteNavController on that navigation controller:

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navController = mainStoryboard.instantiateViewController(withIdentifier: "lastMinuteNavController") as! UINavigationController
// The top view controller is your content VC
// let detailsVC = navController.topViewController as! DrivingDetailsViewController
present(navController, animated: true)

If you only want the content view controller without its navigation wrapper, set the Storyboard ID directly on the content view controller and instantiate that ID instead. Do not set the ID on both and expect either to work interchangeably.

4. Clean up typos, case, and stale builds

  1. Copy-paste the Storyboard ID from Interface Builder into code to avoid typos. drivingDetails != DrivingDetails.
  2. Product > Clean Build Folder (Shift+Command+K), delete the app from the simulator, and rebuild. Storyboards are compiled to storyboardc files and a stale cache can hide your change.
  3. If you use storyboard references, open the referenced storyboard and set the ID there. The main storyboard does not forward identifiers.
  4. Check for duplicate scenes. If you duplicated a view controller, you may have set the ID on the copy, not the one in the navigation flow.

How to prevent it

  • Use the same string constant for Storyboard IDs in code and Interface Builder. Define static let storyboardID = "drivingDetails" on the view controller and copy that value into the Identity inspector.
  • Prefer segues when you can. If the flow is static, performSegue(withIdentifier:sender:) with a segue identifier avoids manual instantiation entirely.
  • Keep one storyboard per feature and name them explicitly. When you instantiate, always use UIStoryboard(name:bundle:) with a constant so the source is obvious.
  • After adding a Storyboard ID, build and run immediately to catch the crash early rather than after more changes.
  • Add a quick assertion in debug builds to fail fast with a clear message if the ID is missing.

FAQ

Why does setting the segue identifier not fix it?

The segue identifier identifies the transition arrow, not the destination view controller. instantiateViewController(withIdentifier:) only searches Storyboard IDs set in the Identity inspector on the view controller itself. You need both if you use both APIs: a segue identifier for performSegue and a Storyboard ID for instantiation.

Do I need to set Restoration ID too?

No. Restoration ID is for State Restoration, not for instantiation. Some developers set it to the same value and check “Use Storyboard ID” to keep them in sync, but setting Restoration ID alone will not fix doesn't contain a view controller with identifier. Set Storyboard ID.

Why does it work on one device or scheme but not another?

You likely have multiple storyboards or targets. One target may include a storyboard where the ID is set, another may not. Check Target Membership for the storyboard file, and verify you are not loading a different storyboard name in a different build configuration or for iPad vs iPhone.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.