Case Silva
Case Silva

Reputation: 514

Cannot add Outlet connection from Button to ViewController

I am trying to create an IBOutlet connection/reference from my button to my UIViewController in Xcode, but it is only giving me the "option" to add an Action connection. I am trying to utilize the viewDidLoad and baselineAdjustment to vertically center the buttons label when the text scales to fit the width, but am not having any luck.

Does anyone have a suggestion or know what I'm doing wrong?

enter image description here

EDIT: Adding a few more screenshots for clarity

If I select my main Scene View, the class is of type ViewController. If I select the view that the Button in question is a part of(it is in a ContainerView), it is of class UIViewController and the options do not present a choice of ViewController.

enter image description here enter image description here

Upvotes: 2

Views: 2870

Answers (3)

AppleCiderGuy
AppleCiderGuy

Reputation: 1297

I see the you have multiple ViewControllers in storyboard. Ideally, each View controller in the storyboard is supposed to be of only one type of UIViewController implementation and it's also true the other way around. so, If you have say 3 UIViewControllers in Your storyBoard, then you will need to create 3 .swift files which implement UIViewController like so:

 abcVC:UIViewController { .....
 efgVC:UIViewController { .....
 ViewController:UIViewController { ..... //this is the default one you get.

and then set the class of each ViewController in your storyboard to one of each of the above.

if your ViewController which has the button you want to outlet has a class type abcVC, then you can outlet your button only in abcVc's implementation in abcVC.swift.

Hope it makes sense. to see how to set class, refer @Vadim F. 's answer.

and if you happen to upvote this answer, please also consider upvoting @Vadim F. 's answer.

This is how you can crate a new .swift file while subclassing a UIViewController: File -> new -> File -> Cocoa touch class -> #make it subclass of UIViewController and give it a unique name eg: abcVC

Upvotes: 1

Vadim F.
Vadim F.

Reputation: 1051

Make sure the right class subclassing the UIViewController, make sure when you are doing it, you choosing the UIViewController and not the View or the safe area.

enter image description here

Let me know if it solved it

Upvotes: 6

Gabriel Henrique Paul
Gabriel Henrique Paul

Reputation: 51

Certify that your ViewController in the Storyboard is of the class ViewController (i.e your view controller) and not from UIViewController.

Upvotes: 1

Related Questions