I have a simple UILabel, nothing special about it, in a UIView inside a UIScrollView. I can link it up to my ViewController.swift file just fine, and it doesn't crash on opening, but whenever I try to use the outlet, it produces a nil. I've been looking around for a solution and it seems as though you can't access an outlet inside a subview from the superview... but nothing has been exactly my situation, and none of the provided solutions work.
Here is the full error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
The strange thing, though, is that I have another UILabel, seemingly the exact same, that works fine when I try to edit it. I do not know what is going on in the slightest here. How can I fix this?
"right click" no your label in storyboard and check if you don't have another outlet connected, maybe you deleted from code and connection is still there, other than that, you can delete the label from storyboard and recreate connections.
Try providing the #IBOutlet as not weak and try.
This is not the right way, but still let us see if it's working.
Related
This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 2 years ago.
I'm new to Xcode and was trying to make a simple code that would unhide a label when a button is pressed. I tried using the same code in a test project with one view controller and it worked just fine, but when I use it in a bigger project with 15+ view controllers I get the error. My outlets seem to be correct so I am not sure what is making the label nil.
Code that I used
Fixed it! Just manually initialized the object as hidden under drawing.
This error seem to be caused by the Outlet connecting problem to me,
I would suggest you to double check the storyboard & viewController connection.
To do so, you can go to your storyboard, and check on the all outlet connector in that scene.
If there are yellow sign, that mean that are the problems.
enter image description here
But if the outlet are correctly connect, try to move that line of code to ViewWillAppear, and see if the error still occurs.
TL;DR: I have an IBOutlet (UILabel) that is properly connected in Storyboard. Accessing (unwrapping) it works fine in viewDidLoad() ... but a few seconds later its value is nil. A watchpoint says that the outlet changes right after a call to viewDidLayoutSubviews(), but... a print() call at the end of viewDidLayoutSubviews() shows it non-nil.
This is very similar to IBOutlet is nil but his solution (track value with didSet()) did not work. (There are many related posts but all had trivial solutions.)
What I have done:
Reconnected the outlet to the Label — from both sides
Deleted the Label and reconnected it
Cleaned the project
Deleted the DerivedData
Quit and restarted Xcode
Quit and restarted my Mac
Added a didSet() method to the outlet.
It triggers during viewDidLoad() and shows a non-nil value.
In viewDidLoad() I set its text value with no problem
It does not trigger before the nil-unwrapping crash
Added a watchpoint to the variable.
This does trigger before crash (right after viewDidLayoutSubviews() and shows the following:
As mentioned before, at exit of the most recent method call before the watchpoint (viewDidLayoutSubviews()) the outlet is non-nil.
These crashes seem always to involve subviews of a particular superview. I added a watchpoint to the superview's outlet but it never triggers.
What could be inciting my code to let go of this weak reference?
TL;DR: I was clobbering the relevant views.
Well, the best way to find your own answer is to ask someone else. Thanks to #DonMag, I was preparing more traces, breakpoints, and screen shots. I noticed this method (added to fix another bug, of course):
Of course, the two views that were becoming nil were subviews of centralOverlay. Ones I wanted to keep.
So I guess my answer was similar to that of IBOutlet is nil — pilot error — except the didSet() handler did not solve the mystery.
I'm using Xcode 6.3.2 with Objective-C.
I hook some label to my header file, and I choose outlet collection. Then, I try to hook another label to the same collection, and it's disabled in any way. I'm try to hook them from both sides, and nothing. There's a workaround I've figured out, which is also mentioned here:
Wiring up Outlet Collection Trouble
The next label I hook, I manually write the same name of the previous outlet collection, then, obviously it creates a new line with the same names, it's a duplicate error, I delete this line, and the labels are hooked in the same collection. It's a workaround, and I don't like it too much. Any ideas of a proper solution?
It appears there is some bug when updating Xcode I guess. I've found out, that actually I can't connect any type of outlets, and I searched for an answer about this, and here's what I've found:
xcode-6-cant-connect-any-iboutlet-to-viewcontroller
This did solve my problem, but still, it's a major workaround. If anyone knows about a proper solution to this issue, I'd like to hear about that.
Update: with 2 downvote of this question, I'd like to make this question a little bit useful to others - since I don't have the choice to delete it. The mistake I made was cut and paste codes which has interface outlet. As I was completely new at that time, I was assuming that when I copy and paste, the outlet link will be copied and pasted as well. Obviously it doesn't work that way.
I was writing a single-viewed app. It has one UITextField and one MKMapView. I want to do something when Return key is hit, so I basically followed
How to hide keyboard in swift on pressing return key?
But it does not fit well with my other codes. Any idea why it isn't working and how to fix it?
Make sure you connect your UITextField from StoryBoard to your searchText IBOutlet by control-dragging from the StoryBoard to the the searchText variable.
You have your outlet set up as implicitly unwrapped. That's the correct thing to do, but when your code executes the outlet must not be nil or your code will crash.
You probabably have a broken outlet link. Set a breakpoint and examine the outlet.
You can change your code to use an "if let" expression to prevent crashes. Search in the Swift language reference IBook for "Optional binding" to learn about it.
Edit:
The code might look like this:
if let requiredSerachText = searchtext
{
requiredSearchText.delegate = self
}
I'm having a problem with setText method for UITextView.
As I said on the title, I tried to change a UITextView text by using setText method or change the text property directly. It only works from the second time since the method is called.
My UITextView was an outlet. I even tried to change it's text directly from the owner class or create a method to call from another class, but it behaves the same.
I dont know if I'm doing sth wrong when create it as an outlet, I also tried to set it as nonatomic, strong, weak, retain but I still can't get it.
Any advice for my case? Thanks in advance! :)
EDIT:
I figured it out from David H's answer.
As my app is using tab, first tab is used for searching words, the second one for displaying the meaning, I tried to set the text before the outlet is created (as I haven't clicked the second tab yet). If I click the tab meaning first in order to let the outlet to be created, then it works perfectly.
Thanks for all the answer!
Almost for sure, the first time you try to set it the outlet is nil - not yet set. So add an assert (assert(myTextView) before you set it, or at least a NSLog message. You will surely find that the textView is nil the first time you try.