How do I set the initial first responder per view in a cocoa app that switches between different views?

135 Views Asked by At

My Cocoa App uses one ViewController. I do not use the InterfaceBuilder On app launch a view will be created and the user can do stuff. When clicking a specific button the VC (as the view's delegate) receives a message and then replaces the view with another.

In this new view I want a specific UI element to be the first responder. So far I have not been successful.

The new view has a reference to the desired element (a subview), so the VC can pass it to the window's makeFirstResponder(:_) method.

I tried to do that in the following places:

  • at the end of the view's init
  • in the view controller's viewWillAppear()
  • in the VCs viewDidAppear()

in the latter two I tried:

if let myView = self.view as? MyView {
    ... here I try to set the UI element as firstResponder ...
}

But in any case I get the following Message:

[General] ERROR: Setting <NSTableView: 0x7f8c1f840600> as the first responder for window <NSWindow: 0x7f8c1ef0efc0>, but it is in a different window ((null))! This would eventually crash when the view is freed. The first responder will be set to nil.

So it appears that at the time I try to set the firstResponder the new view has not yet been attached to the window.

What I also tried is to override the MyView's becomeFirstResponder()method, assuming that when the view is finally presented in the window it will receive that command, but unfortunately this method does not get called.

Is there an easy way to specify an entry point for the responder chain / key view loop per view?

0

There are 0 best solutions below