Connect Label to View Controller

Hi everyone, I'm coding my first ios app and I want to add a label to the view controller by pressing "alt" and dragging it into the code. With a button this works perfectly but with a label it won't work. Any ideas why? Or how I can do it in another way? Thanks

 I want to add a label to the view controller

Do you mean adding an IBOutlet for the label that you have create in the storyboard ?

with a label it won't work.

What happens exactly ?

Likely solution:

Option-drag (alt) will duplicate the object (button, label) in the storyboard. That's not correct here.

You have to control-drag from the Label (in the storyboard) to the code, inside the class.

You will get a popup window asking to give a name to the IBOutlet.

Note: Check that you have given the right class to the ViewController in the Storyboard: it must be the exact same name as the class in which you want to create the IBOutlet.

What is the name of the class where you want to declare the IBOutlet ?

What is the class of the ViewController in which you have define the Label in storyboard ?

They must be exactly the same:

In this example you see:

  • the class is named FirstViewController
  • When you select the corresponding viewController in storyboard, you in Identity Inspector that its class is FirstViewController (and not the default UIViewController)
  • That allowed to create a UILabel by control-drag from the Label in the view in the storyboard.

Have you checked that the 2 names are exactly the same ?

Connect Label to View Controller
 
 
Q