Danny Wang Danny Wang - 3 months ago 14
Swift Question

Swift: unexpectedly found nil while unwrapping an Optional value when setting delegate for UITextField 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?

code

Answer

Make sure you connect your UITextField from StoryBoard to your searchText IBOutlet by control-dragging from the StoryBoard to the the searchText variable.