Insert text into NSTextView/NSTextStorage does not work

  • Hi

    I am pretty new to cocoa and try to refill a NSTextView interface object with new text, but it just does not want to work...

    I dropped the "Text View" (NSScrollView) item out of the "Cocoa"->"Views & Cells" folder onto the application.
    I declared
    IBOutlet NSTextView* registers;
    in my class in XCode, loaded the class into the application builder and connected an object of my class with the NSTextView part of the item I dropped onto the application (from object to item).

    This seems right to me (at least it does work :D), but whatever I want to do with "registers" does not work...

    As I mentioned before, my aim is to replace the whole text with a new (C-)String/char-construct (char*).

    I tried
    [[registers textStorage] replaceCharactersInRange:NSMakeRange([[registers textStorage] length], 0) withString:@"Hi. Nur ein Test "];
    as a little test. Does not work.
    A little bit shorter
    [[registers textStorage] replaceCharactersInRange:NSMakeRange(0, 0) withString:@"Hi. Nur ein Test "];
    also no effect...

    I played around with objects of the type NSAttributedString, creating, trying to use the inherited methods of NSTextStorage from NSAttributedString to insert the NSAttributedString into the TextStorage - does not work.

    At the moment, I tried something very simple:
    NSTextStorage* store = [registers textStorage];
    I inspected it in the debugger and it says "0x0" for the value... well seems wrong to me!
    Next line:
    NSString* blubb2 = [store string];
    This should then return the current content of the textbox as far as I know, actually the variable contains value:"0x0";Summary:"nil"... well, seems incorrect to me too (comprehensible at least - a message to a 0x0 object can not return any appropriate value~).

    In the meantime I am really clueless what might be wrong, and I hope someone here might know what I am doing wrong or might have missed? :)

    Best regards,
    Uli
    --
    Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
    Browser-Versionen downloaden: http://www.gmx.net/de/go/browser
  • It sounds like your NSTextView* is actually connected to the scroll
    view, not the text view. This is an easy mistake to make in Interface
    Builder if you are unfamiliar with hooking up the outlets.
    You can bulletproof it like this:

    assert([registers isKindOfClass:[NSTextView class]]);

    So you assert if the object isn't the kind you expect.

    Uli Gabor wrote:
    > Hi
    >
    > I am pretty new to cocoa and try to refill a NSTextView interface object with new text, but it just does not want to work...
    >
    > I dropped the "Text View" (NSScrollView) item out of the "Cocoa"->"Views & Cells" folder onto the application.
    > I declared
    > IBOutlet NSTextView* registers;
    > in my class in XCode, loaded the class into the application builder and connected an object of my class with the NSTextView part of the item I dropped onto the application (from object to item).
    >
    > This seems right to me (at least it does work :D), but whatever I want to do with "registers" does not work...
    >
    > As I mentioned before, my aim is to replace the whole text with a new (C-)String/char-construct (char*).
    >
    > I tried
    > [[registers textStorage] replaceCharactersInRange:NSMakeRange([[registers textStorage] length], 0) withString:@"Hi. Nur ein Test "];
    > as a little test. Does not work.
    > A little bit shorter
    > [[registers textStorage] replaceCharactersInRange:NSMakeRange(0, 0) withString:@"Hi. Nur ein Test "];
    > also no effect...
    >
    > I played around with objects of the type NSAttributedString, creating, trying to use the inherited methods of NSTextStorage from NSAttributedString to insert the NSAttributedString into the TextStorage - does not work.
    >
    > At the moment, I tried something very simple:
    > NSTextStorage* store = [registers textStorage];
    > I inspected it in the debugger and it says "0x0" for the value... well seems wrong to me!
    > Next line:
    > NSString* blubb2 = [store string];
    > This should then return the current content of the textbox as far as I know, actually the variable contains value:"0x0";Summary:"nil"... well, seems incorrect to me too (comprehensible at least - a message to a 0x0 object can not return any appropriate value~).
    >
    > In the meantime I am really clueless what might be wrong, and I hope someone here might know what I am doing wrong or might have missed? :)
    >
    >
    > Best regards,
    > Uli
    >
  • Hi

    Yeah... well it was connected the right way, but... I am stupid :D

    I tend to have such "tests" working without clicking a button - means, my code was in the init method of the object.
    In my euphoria of coding I did not even realized, that my application window is not even open...
    Changed the init method to awakeFromNib and now it works :)

    I had the same problem with Delphi in school two years ago: The interface item was working seamlessly with every other student, even my teacher did not see what I was doing wrong, and eventually I figured it out by myself~

    Thanks anyway,
    I already forgot how I got this idea now, but the mail helped ;)

    Best regards,
    Uli

    -------- Original-Nachricht --------
    > Datum: Sat, 29 Dec 2007 07:27:13 -0800
    > Von: John Stiles <jstiles...>
    > An: Uli Gabor <gg15...>
    > CC: <cocoa-dev...>
    > Betreff: Re: Insert text into NSTextView/NSTextStorage does not work

    > It sounds like your NSTextView* is actually connected to the scroll
    > view, not the text view. This is an easy mistake to make in Interface
    > Builder if you are unfamiliar with hooking up the outlets.
    > You can bulletproof it like this:
    >
    > assert([registers isKindOfClass:[NSTextView class]]);
    >
    > So you assert if the object isn't the kind you expect.
    >
    >
    > Uli Gabor wrote:
    >> Hi
    >>
    >> I am pretty new to cocoa and try to refill a NSTextView interface object
    > with new text, but it just does not want to work...
    >>
    >> I dropped the "Text View" (NSScrollView) item out of the "Cocoa"->"Views
    > & Cells" folder onto the application.
    >> I declared
    >> IBOutlet NSTextView* registers;
    >> in my class in XCode, loaded the class into the application builder and
    > connected an object of my class with the NSTextView part of the item I
    > dropped onto the application (from object to item).
    >>
    >> This seems right to me (at least it does work :D), but whatever I want
    > to do with "registers" does not work...
    >>
    >> As I mentioned before, my aim is to replace the whole text with a new
    > (C-)String/char-construct (char*).
    >>
    >> I tried
    >> [[registers textStorage]
    > replaceCharactersInRange:NSMakeRange([[registers textStorage] length], 0) withString:@"Hi. Nur ein Test "];
    >> as a little test. Does not work.
    >> A little bit shorter
    >> [[registers textStorage] replaceCharactersInRange:NSMakeRange(0, 0)
    > withString:@"Hi. Nur ein Test "];
    >> also no effect...
    >>
    >> I played around with objects of the type NSAttributedString, creating,
    > trying to use the inherited methods of NSTextStorage from NSAttributedString
    > to insert the NSAttributedString into the TextStorage - does not work.
    >>
    >> At the moment, I tried something very simple:
    >> NSTextStorage* store = [registers textStorage];
    >> I inspected it in the debugger and it says "0x0" for the value... well
    > seems wrong to me!
    >> Next line:
    >> NSString* blubb2 = [store string];
    >> This should then return the current content of the textbox as far as I
    > know, actually the variable contains value:"0x0";Summary:"nil"... well,
    > seems incorrect to me too (comprehensible at least - a message to a 0x0 object
    > can not return any appropriate value~).
    >>
    >> In the meantime I am really clueless what might be wrong, and I hope
    > someone here might know what I am doing wrong or might have missed? :)
    >>
    >>
    >> Best regards,
    >> Uli
    >>

    --
    Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
    Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today