-1

I'm debugging code to see why a WKWebView's instance doesn't become the firstResponder by doing the following:

override func becomeFirstResponder() -> Bool {
    let becomeFirstResponserResponse = super.becomeFirstResponder()
    print(becomeFirstResponserResponse, self.isFirstResponder)
    return becomeFirstResponserResponse
}

This prints out true, false. I've also verified that the web view's window property isn't nil.

As per Apple's documentation, becomeFirstResponder returns true if this object is now the first responder; otherwise, false.. So I'd expect self.isFirstResponder to be true. What am I understanding incorrectly?

Kunal Shah
  • 1,071
  • 9
  • 24

1 Answers1

1

Because you haven't even finished returning from becomeFirstResponder yet! If you were to do your print inside a half-second delay you would find that we are in fact now returning true from isFirstResponder as well.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks, @matt! I'll try adding the delay and seeing what happens. Is this behavior documented somewhere? – Kunal Shah Aug 04 '22 at 20:24
  • 2
    It's not "behavior". You're in the middle of the call to `becomeFirstResponder`. That needs to take effect. – matt Aug 04 '22 at 20:25