iOS development
view controller
Xcode tutorial
Swift programming
app development

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:

  1. Choose File and then New File
  2. Select Cocoa Touch Class
  3. Name it something like SettingsViewController
  4. Set the subclass to UIViewController
  5. Save it in the right group or folder

The file usually starts like this:

swift
1import UIKit
2
3final class SettingsViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6        view.backgroundColor = .systemBackground
7    }
8}

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:

swift
1import UIKit
2
3final class SettingsViewController: UIViewController {
4    @IBOutlet private weak var titleLabel: UILabel!
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        titleLabel.text = "Settings"
9    }
10
11    @IBAction private func closeTapped(_ sender: UIButton) {
12        dismiss(animated: true)
13    }
14}

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:

swift
1let storyboard = UIStoryboard(name: "Main", bundle: nil)
2let controller = storyboard.instantiateViewController(
3    withIdentifier: "SettingsViewController"
4) as! SettingsViewController
5
6navigationController?.pushViewController(controller, animated: true)

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:

swift
let controller = SettingsViewController()
present(controller, animated: true)

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 File instead of Cocoa Touch Class and then forgetting to subclass UIViewController.
  • 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 UIViewController subclass 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.

Course illustration
Course illustration

All Rights Reserved.