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.
Related
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.
This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 5 years ago.
I am new to iOS and currently I am working with container view using tutorial https://kodesnippets.wordpress.com/2015/08/11/container-view-in-ios/
And I want to add one functionality i.e. in Second View Controller I want to add a button and then on click of that button I want to open Third View Controller in the same container view that lies under ViewController.swift.
Now, problem is that when I click on Button in Second View Controller then It shows me fatal exception that I have mentioned in question.
Code for Second View Controller on button's Touch Up Inside
#IBAction func send(_ sender: UIButton) {
container!.segueIdentifierReceivedFromParent("Third")
}
Please help if anyone knows.
I have pushed a new Storyboard with name Secondary.storyboard, with your requirement added. Give it a try. See the attached image.
https://github.com/iaaqib/ContainerView
I have looked at the answers of this question here and none of them seem to fix the problem I am having, also most other answers are not for swift but i tried the ones I understood.
All of the class names match, the outlets have the right names and there are no outlets that aren't defined... no little yellow triangle. (links to images below, site wont let me put more than 2)
Shows class names in inspector and file are the same
Shows outlet names in file and inspector
As the screenshot clearly shows, the property MyInputText does not bind to anything. Did you connect the UITextField in the UIStoryBoard to the related property in the code?
Here a screenshot showing a proper binding between a property and the XIB object.
Here a screenshot showing a proper binding between a property and the XIB object.
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
}
This question already has answers here:
Difference between viewDidLoad and viewDidAppear
(2 answers)
Closed 5 years ago.
Bottom line is, I've been working on an app, and it seems that if I place a UIAlert in viewDidLoad, it gets called twice (from a delegate method of UIImagePickerController). If I put it in viewDidAppear, it gets called once.
I've looked through documentation but it just confuses me.
A UIView object can get loaded into memory and released multiple times without ever getting added to the view stack and appearing on the display.
My guess is that you have 2 references to this view (maybe one in a nib file?), so it's getting loaded, then released when the second reference is loaded and assigned to the same property, then only the latter gets added to the view stack. You can see this by printing out (NSLog) the integer value of self ("%ld",(long int)self) in the viewDidLoad and viewDidAppear methods.