简体   繁体   中英

How do I get objects in the responder chain to handle `insertText:` messages?

I have an NSView subclass which implements acceptsFirstResponder , resignFirstResponder and becomeFirstResponder . It also implements the following method:

-(void)keyDown:(NSEvent *) event {
[self interpretKeyEvents:[NSArray arrayWithObject:event]]; }

I handle the messages that interpertKeyEvent: sends in an appController class (The appController is the NSWindow delegate and therefore part of the responder chain).

This approach works fine for most message generated by interpertKeyEvent: , eg insertNewline: , moveUp: and moveDown: , but it does not work for insertText: .

How do I get objects in the responder chain to handle insertText: messages?

You have to implement the NSTextInput protocol for your class, then insertText will be called. Have a look at nsterm.m in Aquamacs for an example of how to implement it.

According to the documentation the standard implementation by the NSResponder:

"The NSResponder implementation simply passes this message to the next responder, or beeps if there is no next responder."

Two possibilities come to mind: either your view isn't where you expect it to be in the responder chain, or your method signature doesn't match what is expected:

  • (void)insertText:(id)aString

How are you implementing acceptsFirstResponder, resignFirstResponder, and becomeFirstResponder?

Is your application using NSWindowControllers or is it a Document Based application?

Further documentation regarding the event handling architecture:

http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html#//apple_ref/doc/uid/10000060i-CH3-SW2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM