How to connect ViewController.swift to ViewController in Storyboard?

IosXcodeUiviewcontroller

Ios Problem Overview


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: https://stackoverflow.com/questions/19370208/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.

Ios Solutions


Solution 1 - Ios

  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)

Solution 2 - Ios

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

Solution 3 - Ios

Connect storyboard and view controller

You should set ViewController in Class` field

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser83039View Question on Stackoverflow
Solution 1 - IosLachtanView Answer on Stackoverflow
Solution 2 - IosIker SolozabalView Answer on Stackoverflow
Solution 3 - IosyoAlex5View Answer on Stackoverflow