Search code examples
objective-ccocoawebviewnsviewfirst-responder

Is there a robust way to detect first responder changes in subviews?


I'm building a custom view that contains several other subviews (NSTextField, WebView,...). I'd like to have my custom view draw a different border when one of the subviews is the first responder, and act as a single item that can be acted upon with menu items and keyboard shortcuts. It looks something like this:

+-------------+
| NSTextField |
+-------------+
| WebView     |
+-------------+

So far, I've had success subclassing NSTextField and others to notify a delegate when - (BOOL)becomeFirstResponder and - (BOOL)resignFirstResponder are called. This approach doesn't work with WebView though, as it itself contains many subviews--I can't subclass them all!

Is there a better way to detect when subviews change their first responder status? Or a better way to create a custom view?


Solution

  • Both WebViewEditingDelegate method will be call,

    Resign first responder:

    -(void)webViewDidEndEditing:(NSNotification *)notification
    {
    
    }
    

    and when become first responder:

    -(BOOL)webView:(WebView *)webView shouldBeginEditingInDOMRange:(DOMRange *)range
    {
        return YES;
    }