0

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

Community
  • 1
  • 1
Danny Wang
  • 429
  • 9
  • 24

2 Answers2

2

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

chrissukhram
  • 2,957
  • 1
  • 13
  • 13
1

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
}
Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272