Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to change the name of a ViewController class in Swift?

When I change my ViewController class name after it's been created and assigned to a view controller in storyboards I run into problems with the IBOutlets of that view controller, and Xcode does not automatically show the corresponding class in the assistant editor when I click the scene.

I've changed the name of the actual file, the class declaration within the file, and the class of the view controller in storyboards. Am I missing something else?

like image 872
Austin Berenyi Avatar asked Jun 16 '18 15:06

Austin Berenyi


2 Answers

A) If you're using Xcode 9 or later, just select the class name, right click -> Refactor -> Rename

Ex.: class MapViewController: UIViewController - just select the "MapViewController", then right click -> Refactor -> Rename

This will change the name file name, class name and take care of the storyboard class name.

B) If you can't use the method A, then manually rename the class name, file name (.swift), then go to the storyboard and replace the class name of the view controller. You may have issues with the outlets. In this case try to close Xcode and re-launch then remove all IBOutlets and reconnect them again.

like image 69
Swift_Dev Avatar answered Nov 04 '22 20:11

Swift_Dev


  1. Click on the controller name that you want to change.
  2. Inside the class right-click on the class name of the controller.
  3. Now you'll be able to see REFACTOR option.
  4. Click on the refactor you can get RENAME option.
  5. Give whatever name that you want, then click on RENAME.
like image 36
anusha.V Avatar answered Nov 04 '22 21:11

anusha.V