Renaming class causes IBOutlet connection to fail - ios

I renamed a class, which happened to have an outlet connection to a label called mainLabel in a storyboard file. When I run my program, it crashes and gives me the error: "this class is not key value coding-compliant for the key mainLabel".
Other SO answers have shown this to be caused by a bad outlet connection, so I removed the connection and removed any lose ends. When trying to reconnect the label to an outlet in my code, I get the error Could not insert new outlet connection: Could not find any information for the class named CourseTableViewCell
Other SO answers have provided solutions which I have tried and haven't worked including:
Deleting the reference to the file and re-adding it
Cleaning the project
Deleting the derived data folder
deleting the label and re-adding it
Dragging from the code to storyboard rather than vice-versa
I made sure that the correct file name was listed in the identity inspector, under "Custom Class". If anybody has any other ideas, please let me know!
I can provide more information if necessary.

The following steps are what you need to do to find existing IBOutlet / IBAction from and object. Be it label, button, etc. And how to remove it.
Open Main.storyboard. The Document outline window should be visible by default on the left of your storyboard window. If not, click the icon on left of the storyboard window, next to the View As: iPhone 7.
Select the object in storyboard, it's easier to find and then select it from the document outline of the storyboard.
Open the Utility window if not yet open on the most right side. Use shortcut key Cmd-Option-0 (command-option-zero), to toggle open/close of the utility window.
Open the connection inspector by click on right arrow icon (top right most icon). Remove any unwanted IBOutlet / IBAction or any other connection you want to remove by clicking on the x icon next to the connection.
Recreate the connection. You could also drag directly from the circle next to the element in the connection window for a particular object. Else add the connection the way you have been doing for now.
As for the Class file and other coding file. You could just comment out the lines that previously have IBAction or IBOutlet. And retype the same line manually (don't cut and paste). Add back the connection from storyboard, and you should be good. Don't forget to delete the lines you commented out that you don't need anymore.

Related

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.
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?
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.
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.
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).

Textfield appears as an action in Xcode 9

Why my Textfield appears as an IBAction?
check that you set class name in identity inspector tab for viewController
#Kiruthiga check class name of UITextField, and also check u should select IBOutlet instead of Action when u connect connection by dragging.This will help u
The fix is to define the custom class for the view controller you are wanting to connect outlets from inside the Identity Inspector (this same problem happened to me).
Select the view controller with the text fields you want to connect to code.
Go to Identity Inspector in the utilities panel and the first field should say "class" (Identity Inspector Image)
Fill this box with either the name of the file of the code you want to connect to the selected view controller, or you must create a new view controller file. To do this, you can go to File > New > Cocoa Touch Class. Then for "Subclass of" box put UIViewController, name it, then press enter.
The file you just created should come up as a .swift file (make sure this file is within the hierarchy of the app files as it tends to be created outside of it!)
Go into the "Class" box from the Identity Inspector and insert the name of the file you just created (it should come up automatically when you being typing).
Control-Dragging the text field to this file should now give you the option to treat it as an outlet rather than forcing it to be an action!
It's due to that LaunchScreen.storyboard - try in main.storyboard
You can change it according to your requirement :
Select collection type as outlet. Here you selected action which means action s that to be performed on clicking textfiled
Possible ways to get back.
1) Select UITextField if any class name already selected remove/clear it.
2) Maybe It looks like a problem on Xcode builds because of Object as Exit. So please clean and rebuild the project. try close project reopens it.
3) Check u should select IBOutlet/Outlet instead of Action when you connecting connection by dragging.
UITextField Can have IBAction of Editing Changed event. so you don't need to surprise on that part.
If you want to create IBOutlet drag to empty area not to IBAction function.
Hope it is helpful
Check your class...you have to assign a class to that ViewController....after assigning the class the problem will get resolved #kiruthiga

NSUnknownKeyException error in app delegate

My app is crashing on every screen at app delegate where ever I have used label and textfields. When the tags are removed app is working fine but is crashing again when connected to swift file again.
If you have this type of error
STEP 1:Go XIB or if use STORYBOARD go STORYBOARD
STEP 2:Click View Controller
STEP 3:Again Give connection properly(Fill the circle)
STEP 4:Also check whether it is for button action or outlet property
Very Important: Right Click the File's Owner(Yellow Color) and Click on the "Connection Inspector" (upper right arrow), to see all outlets at once. Look for !s which indicates a missing outlet.Remove that. Now it works.

How to connect an IBOutlet and IBAction to a View Controller without using the Assistant editor

I know the second button here shows the Assistant editor:
And I know how to make an Outlet and Action by Control dragging from an object in the Interface Builder to the View Controller code. Ideally it should be as easy as the documentation makes it look:
But this is usually what Xcode actually looks like for me when I press the "Assistant" editor:
A mess. I have to minimize lots of things, try to get the storyboard object in view, and then go find the right View Controller. All this before I can do the Control-drag.
Is there a way to make the connection without using the Assistant editor? (And preferably without having to type a lot of code in myself.)
Don't press the assistant editor button. Sometimes it opens a random file instead of the one you want.
When you are in Storyboard, Option click on the .h file that you want to open in the Project Navigator. This will open the proper .h file to add the outlets or actions.
When you're done, close the Assistant editor right pane (which is displaying the .h file) and you will be back in Storyboard.
Yes, you can do it without the Assistant editor and without writing lots of code. It requires learning to do two things:
Use code snippets
Use the Connections inspector
Create code snippets for the IBOutlet and IBAction
Normally when you create an IBOutlet and IBAction with the Assistant editor it automatically adds code like this to your View Controller:
#IBOutlet weak var myOutletName: UIButton!
#IBAction func myActionName(sender: AnyObject) {
}
You could type this all yourself and then add the connection in the Connection inspector, but why do all that typing? Just make a code snippet that will autocomplete. Drag the code to the Code Snippets library in the Utility panel.
Give it a title and most importantly, a Completion Shortcut. I called mine "ibaction" for the #IBAction code.
Now all I have to do is start typing "ibaction" and I can autocomplete the code snippet. I only have to edit the name of the action. It is a similar process for the Outlet.
Read more about creating code snippets:
Xcode Snippets
Creating a Custom Code Snippet
Now all you have to do is connect the IB object to the code.
Make the connection with the Connections inspector
First, click the object in the storyboard that you want to connect. For me, I am using a Button.
Then select the Connections inspector. It is on the far right.
Alternatively, you can right click or control click the object to get a menu.
Then click the New Referencing Outlet to connect it to your Outlet or the Touch Up Inside under Sent Events to connect it to your Action.
For whatever reason I find that sometimes I need to drag just a little bit after clicking the "+" button to get the menu of available connections to show up.

Where do I detect where the outlet is from in Xcode?

I downloaded a project from GitHub and I'm experiencing some problems when detecting where a referencing outlet comes from.
This is an UIImage called "backgroundImage".
This is a UIViewController called "WalkthroughPageContentViewController".
"backgroundImages" has two outlets references, one of which is connected to the "WalkthroughPageContentViewController" UIViewController.
But there isn't any #IBOutlet connection here.
So where does the first referencing outlet come from?
I had a quick look at this project, and the answer is that the referencing outlet doesn't come from anywhere. It's broken. If you examine the destination, the you'll see that Xcode knows this is a problem:
And if you open up the WalkthroughPageContentViewController.swift file, and add an outlet called backgroundImage, then close and reopen the file, you'll find it's magically been linked back up to the Storyboard:
So, I'd surmise that at some point, there was an outlet in the file called backgroundImage, which was hooked up to the Storyboard, but then it was later deleted, leaving the project in this state.
This is pretty common when editing projects in Xcode. The Storyboard connections are basically just stored in XML in the .xib file, and there's no magic two-way connection between them and the code at design time, so if you delete the lines of code that they point to, you'll end up in this state.

Resources