NSUnknownKeyException error in app delegate - ios

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.

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

Renaming class causes IBOutlet connection to fail

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.

Could not instantiate class named IBNSLayoutConstraint

I'm using XCode6 beta and trying out Swift. When I put some auto layout constraints in a view controller the app crashes with the following error:
Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named IBNSLayoutConstraint'
You're getting this error because you've set a constraint to an IBOutlet that is removed at runtime. This happens when you set the constraint to be a placeholder in Interface Builder. Since the constraint is removed, when it goes to unarchive it, it throws an error saying it can't do so.
There are two ways to correct this.
Method 1
Right-click on your Storyboard > Open As > Source Code
In the opened storyboard xml, search for placeholder="YES".
You'll find constraints that are set to be removed at runtime. Remove the placeholder attribute from the constraint, save and close.
Run the app and your problem should be fixed.
Method 2
Find the constraint that's causing your problems in Interface Builder. Uncheck the Placeholder option in the GUI. This should be one of the constraints that's set to an IBOutlet in the ViewController that's causing your crash.
This is what it should look like:
Alternative
Assuming you actually want the constraint to be a placeholder, then you'll need to remove any referencing outlets. To do this, select the constraint that you wish to be a placeholder. Then open the connections inspector (the button furthest to the right that looks like this: (->) ) and then remove any referencing outlets that may exist on that constraint.
I had the same problem just now and the following worked for me.
I released a working version of my app to the App Store, came back to work on it again a few days later tapping onto one my tabs in the UITabBarController it crashed with the error:
Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named IBNSLayoutConstraint'
I didn't have any placeholder layout constraints that I was aware of or constraint outlets that shouldn't have been defined.
The solution for me was to simply turn size classes off and keep size class data for iPhone (App is only for iPhone). This must've deleted anything I'd missed in the size classes. I want size classes so I turned them back on and the app just worked again.
This might not be necessarily a constraint problem. For me it was caused by not having checked "Installed" for a size class that was applying to my layout, see here
There was some problem with using size classes. I disabled that option from the storyboard properties and use only auto layout. It wasn't such a big problem since the app is iPhone only.
What ended up happening to me was that I cut a subview inside of Interface Builder to copy it into another xib. This left the subview in original xib Interface builder with it being greyed out. After removing the subview from it's original place (by selecting and deleting). I was able to get rid of the error.
There is an option in the inspector window when you select the xib file that will not use the constrains under "Show file the inspector" in the inspector called "Use autolayout".
I made a change to a scroll view so that it would let the picker controls embedded in it work properly using a solution I found elsewhere in Stackoverflow.
My new storyboard simply added these attributes to the scroll view, which seemed fine to me.
delaysContentTouches="NO" canCancelContentTouches="NO"
But in addition, I saw in my storyboard in another scene the following new fragment:
<variation key="default">
<mask key="subviews">
<exclude reference="86H-aM-wei"/>
</mask>
</variation>
I have no idea where it came from. At first I ignored it because everything seemed to work find on my dev machine. But when the build was built as Release and tested, I got the crash. Removing that spurious(?) fragment fixed the crash and has not seemed to impact anything else.
I had this issue when I had a button in a custom UICollectionViewCell, and had some of its constraints as IBOutlets on that class. I moved the button from the cell to the parent view controller and the IBOutlets were still referenced in the cell but didn't actually exist on the cell so blew everything up. Just needed to remove those IBOutlets and everything worked fine again!

additional NIB for view controller in tabbed app

{Xcode 4.2, deployment target iOS4.3, not storyboard, using ARC}
I started with a tabbed application template & chose the universal platform, which nicely gives me view controller classes & NIB files for an iPhone (ClassName_iPhone.xib) & an iPad (ClassName_iPad.xib) for 2 tabs, with an if statement in the AppDelegate to determine which to run - exactly how I wanted it set up.
I wanted to add a 3rd & 4th tabs, so starting with the 3rd tab (doing 1 at a time) I created a new UIViewController subclass. As it doesn't give the option to create both NIBs at once, I selected "Targeted for iPad", & had intended to create the iPhone NIB manually. I added a "_iPad" suffix to the created NIB file, then I created a user interface view NIB file to which I added the "_iPhone" suffix. I then set up the code for the new view controller in the AppDelegate implementation file to include the 3rd view controller & tab, & I used the other view controller classes as a guide to set up the new class's code.
For the 3rd _iPhone NIB, I dragged a view object from the objects library onto the canvas, & set it up as per the other 2 _iPhone NIBs. But when I went to connect the outlets, there is no view outlet in the referencing outlets of the connections panel to connect with, which I thought there should be. At this point I suspected something was wrong.
I tried running it in the simulator, in iPad mode it works fine (all 3 tabs are clickable). But in iPhone mode clicking the 3rd tab crashes it with a "SIGABRT" on thread 1. It's obvious what I did didn't work. I don't see anything in the output window that gives me any clues.
Being a newbie to obj-c, so not being too sure of the problem, I would have thought that I either:
have used the wrong user interface template (view)
should have used a view controller object from the object library
(not a view)
or that I should have declared some outlets in my view controller
class files.
But if I should have done either of the latter 2, then my question would be why does the iPad NIB work then, when it clearly has a view object in the NIB & no outlets declared in the class files (same with the other 2 view controllers for both devices)?
Does the UITabView class somehow have outlets pre-declared within it for the first 2 tabs? But that still doesn't explain why the _iPad NIB works.
As usual, any help & advice much appreciated, & if there's a link to an explanation somewhere that I've missed, please show me, because I'm happy to do the research.
If what I've done wrong here is not determinable, then I guess ultimately what I'm asking is a clue to how best to create the second NIB file for iPhone to mesh with the class created with iPad NIB.
Sorry to answer my own question but with further searching I found this answer that was the solution, although not quite the whole story. So I thought to put what I did in an asnwer so others can refer to it.
As Piotr Czapla explains in the linked answer, for some reason Xcode doesn't populate the connectionRecords data, as you can see by my first red arrow. Having a look at the view controller that works (where second red arrow is), that's what the data should look like. So the answer is to cut the data & paste it into the NIB file, or type it. You can do this in Xcode by right-clicking the NIB file in the project navigatior & then Open As > Source Code, which is what you see in my screenshots.
The bit I want to add to Piotr Czapla's explanation though is the destination reference pointed to by the second red arrow might not be correct for the NIB file you're pasting into (mine wasn't) & Xcode might not let you go back into IB mode. If so, you need to get the correct reference from the IBUIView class within your NIB file, as pointed to by the third red arrow. Once I copied that reference to my destination reference ref=, as shown by the fourth red arrow, all was ok & the problem was solved. I could then go back into IB mode (right click, Open As > Interface Builder - iOS) & the view works in the simulator.

Resources