Adding Swift File to New View Controller in Xcode? (Easy)

Adding swift file to new view controller in xCode? (easy)

  1. Add a new Cocoa Touch class. Choose File->New->File... from the menu. Give this new class a name such as "ViewController2" and set the Subclass of: pop down to UIViewController. The file "ViewController2.swift" will be created.

Sample Image

Sample Image


  1. Click on your new View Controller in Interface Builder. In the Identity Inspector (on the right in Xcode) set the Custom Class for the View Controller to "ViewController2".Sample Image

How do I create a view controller file after creating a new view controller?

Correct, when you drag a view controller object onto your storyboard in order to create a new scene, it doesn't automatically make the new class for you, too.

Having added a new view controller scene to your storyboard, you then have to:

  1. Create a UIViewController subclass. For example, go to your target's folder in the project navigator panel on the left and then control-click and choose "New File...". Choose a "Cocoa Touch Class":

    Cocoa Touch Class

    And then select a unique name for the new view controller subclass:

    UIViewController subclass

  2. Specify this new subclass as the base class for the scene you just added to the storyboard.

    Sample Image

  3. Now hook up any IBOutlet and IBAction references for this new scene with the new view controller subclass.

How to create a view controller file after creating a new view controller? (xcode 8.21)

You chose the wrong path to generate your file. In Xcode select "Cocoa Touch Class" instead of "Swift File" you can then decide if you want a Swift or Objective-C file with a specific Class (UIViewController in this case)
Xcode dialog new File

How to connect ViewController.swift to ViewController in Storyboard?

  1. Choose your ViewController in the storyboard or scene list (mine is called Circuit).
  2. Click on the Identity Inspector on the right
  3. Select Custom Class > Class > Type name of your swift file.

DONE :)

Here are these two steps on image (ugly i know)

How to add the code file for a second ViewController

If you are using Xib then

1) Goto file->New->iOS->Source->Cocoa Touch Class

Sample Image

2) Select UIViewController as your Base Class and name your class and also check the checkbox with "also create Xib File" click next and click finish.

Sample Image

Now you have a new ViewController with a file associated with it.

And if you are using storyboards then follow these steps.

1) Drag and drop a viewController from object library

Sample Image

2) repeat step 1 and 2 of first method but this time do not create an Xib file

3) select your newly draged droped ViewController and assign it the new class as follows

Sample Image
Now you have a new ViewController with an associated swift file.

Do you need a separate .swift file for each view controller in an app?

You have to assign to your new controller in the storyboard a class (inside a .swift file), but you can have multiple controllers with the same class, just add a class to your controller here :

Sample Image

Example :

If you have a Test.swift like this :

//Test.swift
class viewController1: UIViewController {
}

class viewController2: UIViewController {
}

You could assign viewController1 or viewController2 inside your storyboard to your ViewController, however you should always have a single subclass of UIViewController inside your .swift file.

Is it necessary to manually add a ViewController.swift file after adding a new view controller in the story board?

No.

The ****ViewController.swift-files are there for you to be able to react to UI events like clicked buttons, shown / unshown views etc. In the InterfaceBuilder you can assign a ViewController to a view - you do not have to. If you do not set one, a default UIViewController will be used and assigned.

Without a custom one however you can not react to any events or change texts etc. BUT you can always come back later and then add a ViewController to do some customization.

A custom controller can be set by clicking on the top left icon of your view and specifying a different class than the default one:

Sample Image

Cannot connect View Controller to Swift file

In your newly created Swift file. You need to declare a class which inherits from UIViewController.

Like:

class SomeViewController: UIViewController {

}

Then you will be able to connect IBOutlets and IBActions to this class.

If you are still having issues after your class inherits from UIViewController check the file inspector and ensure that the {your app name} target is checked (so that the file belongs to the correct module)

Adding Multiple View Controllers to ViewController.swift From Storyboard UI

If I understand correctly, you have multiple view controllers all with the same class "ViewController" that you want to duplicate and edit all the same way? You have to label each class differently or else the system crashes its like having twins and naming them all the same name and getting confused why they dont know which one you are talking to.



Related Topics



Leave a reply



Submit