-1

I have put a full-screen size invisible button behind of all objects(like textfield, picker.. ) to close opened keyboard. I call below function when the button is clicked:

func hideKeyboard() {
    for view in self.contentViewOutlet.subviews {
        if let tField = view as? UITextField {
            tField.resignFirstResponder()
        }
    }
}

but I get this error after I click the button:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I roughly know what does it mean but I couldn't write a solution. (Actually, this hideKeyboard() function was working fine. It starts to give an error after I add UIPickerView)

Yury Imashev
  • 2,068
  • 1
  • 18
  • 32
mannyCalavera
  • 593
  • 1
  • 4
  • 23
  • You're saying that you get an error when you click a button. But you show us a code that hides the keyboard. What line causes an error? – Yury Imashev Sep 23 '18 at 11:01
  • @Cristik why do you think it's a duplicate? This questing is not about the meaning of the error. – Yury Imashev Sep 23 '18 at 11:02
  • @YuryImashev button calls hideKeyboard() function. And i get an error in this line: for view in self.contentViewOutlet.subviews (Thanks !) – mannyCalavera Sep 23 '18 at 11:02
  • @Cristik Of course, I read some of them first but I couldn't write a solution. It is about ability of writing correct syntax. I am trying to learn – mannyCalavera Sep 23 '18 at 11:05
  • 1
    This code crashes if `contentViewOutlet` is `nil`. Check that. – vadian Sep 23 '18 at 11:05
  • @mannyCalavera there is an answer to the question I mentioned that says about unconnected outlets. – Cristik Sep 23 '18 at 11:09
  • I have copied the source code of this page from another page. I forgot to create outlet for view like you said Vadian and Cristik. And it is better if I write it with guard like you said @YuryImashev. Thanks!! – mannyCalavera Sep 23 '18 at 11:16
  • 1
    If you just want to the currently editing field to resignFirstResponder, use `view.endEditing(force: Bool)` – Ashley Mills Sep 23 '18 at 11:23

1 Answers1

2

Your contentViewOutlet is an Outlet so it might be nil, but it's implicitly unwrapped. And you get this error because when you tap a button, this object is nil. To avoid the crash, change your code to

func hideKeyboard() {
    guard let contentView = self.contentViewOutlet else { return }
    for view in contentView.subviews {
        if let tField = view as? UITextField {
            tField.resignFirstResponder()
        }
    }
}

After that, your method won't do anything if contentViewOutlet is nil.

Yury Imashev
  • 2,068
  • 1
  • 18
  • 32
  • This just avoids the crash. This does not fix the problem. Why is `self.contentViewOutlet` nil? Fix that problem instead of working around it. – rmaddy Sep 23 '18 at 16:50
  • @rmaddy Agree. I've thought about that too. But there was no question about how to fix this problem and not enough info to give some advice so I decided to simply help to explain the problem and show how to avoid the crash itself. – Yury Imashev Sep 23 '18 at 16:56