How do I create a view controller file after creating a new view controller?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Creating a new scene in a storyboard does not automatically create its Swift file. To give that view controller code-behind, you create a UIViewController subclass, assign that class to the scene in Interface Builder, and then connect outlets or actions as needed.
Create the Class File
In Xcode, add a new file and make it a UIViewController subclass.
For Swift:
- Choose
Fileand thenNew File - Select
Cocoa Touch Class - Name it something like
SettingsViewController - Set the subclass to
UIViewController - Save it in the right group or folder
The file usually starts like this:
At this point, you have created the code file, but the storyboard scene still does not know about it.
Assign the Class in Interface Builder
Open the storyboard, select the view controller scene, and open the Identity Inspector. In the Class field, set the custom class to SettingsViewController.
That step is what links the storyboard scene to the Swift file. Without it, the scene still uses a plain UIViewController.
If you plan to instantiate the scene from code, also assign a Storyboard ID in the Identity Inspector, for example SettingsViewController.
Connect Outlets and Actions
Once the scene is linked to the class, you can connect UI elements.
Example:
Control-drag from the storyboard element to the code file to create the @IBOutlet and @IBAction connections.
Instantiate It from Code
If the scene has a Storyboard ID, you can load it programmatically:
That works only if the class and identifier are configured correctly in Interface Builder.
Creating the Scene Entirely in Code
If you did not start with a storyboard scene, you can still create the file first and present it programmatically:
In that setup, there is no storyboard class assignment step because the Swift type itself is the source of truth. The original question usually comes from storyboard users, but it helps to remember that UIKit supports both workflows.
Keep the File and Scene Names Aligned
Xcode does not require the class name, storyboard ID, and scene title to match, but your project becomes much easier to navigate when they do. A controller named SettingsViewController should usually have a storyboard ID with the same name and live in a file with that same class name.
Common Pitfalls
- Creating the Swift file but forgetting to assign it as the scene's custom class.
- Using
Swift Fileinstead ofCocoa Touch Classand then forgetting to subclassUIViewController. - Forgetting the Storyboard ID when trying to instantiate the controller from code.
- Renaming the Swift class without updating the storyboard, which causes runtime loading errors.
Summary
- Create a
UIViewControllersubclass file in Xcode. - Assign that class to the storyboard scene in the Identity Inspector.
- Connect outlets and actions after the class is attached.
- Add a Storyboard ID if you want to instantiate the scene from code.
- The key link is not the file's existence by itself, but the custom class assignment in Interface Builder.

