Where is the .swift file for new ViewController?

Ok, newby alert.


So sorry if I don't get termiology correct while I try to describe what I'm attempting to do.


I'm kinda new to XCode and have created a quick app.

In the Main.storyboard file I've added a few buttons and connected them to code (outlets and actions) in the ViewController.swift file by Control/Dragging to the ViewController.swift file.

The outlets and actions are added just fine and things seem to work well.


No I want to add a new ViewController, connect the first to the second via a Seque and navigate to it from a button located on the first ViewController.


I've done this and things navigate just fine (I can click the correct button on the first ViewController the second ViewController is shown and visa-versa).


Now I want to add some buttons to the second ViewController and connect them to code (outlets and actions).


However, I don't see an associated ViewController.swift file to Control/Drag to to add outlets and actions for the controls I plan putting onto the second ViewControlller.


What am I doing wrong?


Thanks


-Mike

You need to create the .swift file for the second view controller. In Xcode choose File > New > File to create a new file and add it to your project. Select either Cocoa Touch Class (iOS) or Cocoa Class (Mac). Click the Next button. Name the class and make it a subclass of either UIViewController (iOS) or NSViewController (Mac).

Thanks szymczyk


Ok, I created the file, and set it to a type of UIViewController.

The file was created just fine.

In the tutorials on Apple's site I've been reading, they have me enable the Assistant editor with the storyboard in the left pane and the source code in which the outlets and actions are to be placed in the right pane.

After some finagling and hair pulling in the Assistant Editor, I was able to get the main story board with the new ViewController in the left pane and the new source file in the right pane as expected.

I then added a Label to the ViewController in the left pane and and selected this label.

However, when I Control/Drag from the selected label in the left pane to the new source file on the right, XCode does not highlight anywhere in the file on the right, which it normally does in order to let me know where I can place an outlet or action.


Am I doing this whole process incorrectly? 😊


Thanks again szymcsyk


-Mike

Accepted Answer

When you created the file for the second view controller, you created a subclass of UIViewController. Now you have to go to the storyboard and set the view controller's class to the subclass you created. The class names have to match for you to make the connection by control-dragging.


Go into the storyboard and select the second view controller. Open the identity inspector by choosing View > Utilities > Show Identity Inspector. Enter the name of your subclass in the Class text field.

Excellent! 😀


That worked.

Thank you!


One of the problems I was having was that when I would attempt to select the new ViewController in the storyboard, I'd click in the center of the control. This would always indicate UIView in the Class text field, not allowing me specify the name of my new subclass.

However, if I click the top of the control in the title-bar area of the new ViewController, then it allows me to specify my new subclass I just created.


Thanks again szymczyk 🙂


-Mike

Where is the .swift file for new ViewController?
 
 
Q