How to set the text of an NSTextView

Discussion in 'Mac Programming' started by printf, May 18, 2010.

  1. printf macrumors regular

    Joined:
    Aug 27, 2008
    #1
    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?
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    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.
     
  3. printf thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #3
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.
     

Share This Page