Renaming View Controllers Without Breaking the Scene

“Can’t refactor Swift code. Xcode can only refactor C and Objective-C code.” Ugh. I wonder where AppCode is with this…

When creating a new Tabbed Application from the Xcode templates, one of the first things that you’ll want to do is rename the view controllers that are generated for you to something that actually makes sense. Unless, of course, FirstViewController and SecondViewController describe the scenes you’re building. In a lot of the IDEs I use for other languages, something like this is as simple as Refactor -> Rename. Currently, that isn’t an option in Xcode for Swift projects.

So, what happens when you rename the view controller and view controller file? Nothing. Or, so it seems until you start building out the controller and none of your outlets or actions are available to you via Connections. This can be a bit of a head scratcher for someone who hasn’t encountered it before, so below are the rest of the necessary steps for renaming your view controller without disconnecting it from its scene:

  1. Rename your view controller class name and file name
  2. Select the scene that is using the view controller in the Document Outline
  3. In the Identity Inspector, select the renamed view controller in the Class field’s dropdown
And, done!
Share