Jane Liu
Jane Liu

Reputation: 61

Cannot connect to IBOutlet in a new custom class Xcode 11

I am working on an iOS project in Xcode 11 to create an app that converts Farenheit to Celsius. I deleted the default ViewController.swift file and created a custom class with a more descriptive name called ConversionViewController.swift. This file was then saved to the current project. The file contains these lines of code:

class ConversionViewController: UIViewController {

    @IBOutlet var celsiusLabel: UILabel!

    @IBAction func fahrenheitFieldEditingChanged(_ textField: UITextField) {
        celsiusLabel.text = textField.text
    }
}

In Main.storyboard I selected the View Controller in the document outline then opened the identity inspector. In the Custom Class section of the identity inspector I changed the class to ConversionViewController and below the module field I checked Inherit module from target. Next, I want to create an outlet to the Celsius text label and create an action for the text field to call when the text changes. So, I open Main.storyboard and try to control-drag from the Conversion View Controller in the document outline to the Celsius label and connect it to the celsiusLabel in ConversionViewController.swift.

The problem is that Xcode won't let me. When I control-drag from the Conversion View Controller to the Celsius label nothing happens. I don't get a pop-up box that gives me the option to select the outlet celsiusLabel.

I've tried going to Product --> Clean Build Folder but that didn't fix the problem. Is there a step missing? I am using the iOS Programming: Big Nerd Ranch Guide 6th edition, which I know isn't written for Xcode 11.

Upvotes: 1

Views: 2343

Answers (5)

Samsi
Samsi

Reputation: 11

I solved it by refactoring the name of the ViewController instead of just renaming it.

Control-click the symbol and choose Refactor > Rename.

Upvotes: 1

Bowdus
Bowdus

Reputation: 33

This typically happens to me when I move or rename a file. The fix in those circumstances is to remove the file from the 'compile sources' list, then re-add. Then build (no need for clean build).

Upvotes: 0

Apps Maven
Apps Maven

Reputation: 1410

I have faced the same problem:- It will be resolved by two ways:-

1) After changing the name of the .swift file just clean the build. If it not lets you create outlet then just quit the xcode and run it again then it will allow you to add outlet to the class.

2). In the connections Inspector select the circle coming under Referencing Outlets and drag that to the viewController to create outlet of the control.

Upvotes: 0

Atmaram
Atmaram

Reputation: 634

In your StoryBoard select the ViewController by clicking on top where the name is displayed after that select the connections inspector (last option). You will see your celsius label there under outlets. Drag from the label's empty circle there to the label in the ViewController's View and connect.

Upvotes: 0

matt
matt

Reputation: 534885

When I control-drag from the Conversion View Controller to the Celsius label nothing happens. I don't get a pop-up box that gives me the option to select the outlet celsiusLabel.

Yes, I often see that problem. Usually I can solve it by dragging a different way. For example, select the label, switch on the right to the Connections inspector, and drag from the circle next to New Referencing Outlet onto the view controller in the outline. Now does it let you make the connection?

Upvotes: 0

Related Questions