60

I made a new view controller in Storyboard and made a new .Swift file that inherits UIViewController. However, my viewDidLoad() is not being called.

What do I need to do to connect my view controller in my story board to my .swift code so viewDidLoad() is called?

I see there's this: How to connect storyboard to viewcontroller

However, in a default project, FirstViewController(storyboard) doesn't have FirstViewController in that box, it is empty, yet viewDidLoad() is still called. It doesn't make sense.

1
  • 1
    The link provided it’s correct, you just choose in the Class field the right Swift file; Commented Oct 13, 2014 at 11:06

3 Answers 3

120
  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)

Sign up to request clarification or add additional context in comments.

2 Comments

What if the project is a mix of Obj-C & Swift Classes, and there are Obj-C ViewController Classes and Swift ViewController Classes?
remarkable my xcode ui has all 4 buttons other than the one displayed. Just great.
8

You have your ViewControler created with some Objects in it (UILabel, UIButton, UIImage...)

enter image description here

1 - You need to link your ViewControler in your story board to your ViewController.swift , to do this follow the pictures

enter image description here

2 - In the class filed put the name of the ViewController class. With that you just linked your storyBoard view controller to your viewController.swift class

Class = ViewController.swift

enter image description here

3 - Now you need to cretae the variables you want to asign(UILabel, UIButton ... that you have in your storyboard): in this example:

class MovieDetailViewController: UIViewController, DetailView { var presenter: MovieDetailPresenter! var movie: Movie? @IBOutlet weak var lblMovieYear: UILabel! @IBOutlet weak var lblMovieTitle: UILabel! @IBOutlet weak var movieImage: UIImageView! @IBOutlet weak var lblMovieRating: UILabel! @IBOutlet weak var lblMovieOverview: UILabel! } 

4 - To link the UILabel in the story board to your UILabel variable or your UIButton in your story board to your UIButton var, follow the next images

First select the view controller in the storyboard

enter image description here

Second select the parragraf icon in the right up corner ands clicked 'Assistant', it will open a screen of your ViewControler.swift

enter image description here enter image description here

Now you just need to drag the variable to the corresponding object and you will be done.enter image description here

Remember to do this with all variables, you will need to create a variable for each object you have in the storyboard

1 Comment

awesome explaination
2

Connect storyboard and view controller

You should set ViewController in Class` field

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.