I had created new swift file myviewController.swift
and before linking to my view controller, I had renamed it to newController.swift
. Yet it is not showing in the identity inspector to link the class to my view controller.
I tried adding it directly to the project folder but it says that newController.swift file already exists.
Can anyone suggest what is the problem?
The list of classes populated in your Interface Builder is based on the class names and not the file names. Single files can hold multiple classes.
In addition to changing the file name, you need to also change the class name if you want the class name to be different in Interface Builder and other locations.
If, after changing the class name, it still does not appear in Interface Builder, try building your project by pressing Cmd+B, and looking again.
Consider the following file:
import Foundation
import UIKit
class FooController: UIViewController {
//stuff
}
class BarController: UIViewController {
// stuff
}
Despite the file being named ViewController.swift
, ViewController
will not appear in the drop down list for my view controllers.
Instead, I get default, built in UIKit
view controllers, and the two controllers I defined in this file, FooController
, and BarController
:
Renaming the file in Swift 2.1, XCode 7 , iOS9 is done in 3 steps:
Step 1 : Rename the file itself eg: From the default 'ViewController.swift' to 'MyViewController.swift'. Make sure you keep the have the 'ViewController' text appended( in this case to 'My'. thats the Standard.
Step 2: Within the file, change the name of the Class eg: From the default 'Class ViewController : UIViewController' to 'Class MyViewController : UIViewController'
Step 3: In the main storyboard, click/select on the scene/ViewController (make sure you got blue border). Go to the Identity Inspector in the right menu, under the 'Custom Class' section, the 'Class' dropdown should now have the new name i.e 'MyViewController'. Just select it.
Clean the project so that the same is refreshed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With