Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
this should be incredibly simple, but going from win32 and carbon to cocoa has been embarrassingly painful:

i followed this tutorial http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/005-NSTextView.pl which explains how to wire up a key down event (which i got working), but i can't figure out how to reference the textview from my AppDelegate class in order to call

[myTextView setString:mad:"test"];

what am i missing?
 
The tutorial appears to have an instance variable that's a pointer to the text view, wired up via the IBOutlet system. That's the usual way to do it.
 
Overriding keyDown is the wrong approach. It may be simple to understand, because it's how other languages and frameworks often do it.

The Cocoa way is to assign a delegate object to the NSTextView. This object is then called for any of a variety of events and conditions. One of the parameters to the delegate's methods is the NSTextView itself. So you never have to "find" or "remember" the NSTextView, it's always given to your delegate.

And you can assign the same single delegate object to any number of NSTextView objects. There is no rule against delegate sharing, if the design has a use for it.

One of the goals of the Cocoa framework is to reduce the amount of subclassing you have to do. Lots of things that require subclassing in other languages don't need subclasses in Cocoa, because the standard Cocoa class uses delegates.

Refer to the reference docs for the NSTextViewDelegate protocol and its superclass, the NSTextDelegate protocol.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.