1

Below is a simplistic Obj-C / Cocoa "hello world" window, which is initialized from within a Carbon app. The .xib contains a NSWindow, which has an NSView containing the NSButton/NSButtonCell and a NSScrollView/NSTextView/NSScroller(s).

The code compiles and links with no warnings. The window is displayed properly, with both objects (button and text field). Pressing the button does indeed go to buttonWasPressed, and I receive no errors regarding bad selectors in Xcode's debugger.

But the text in the NSTextView is unchanged.

I THINK I have the proper outlet for myTextView connected. Perhaps using replaceTextContainer is not a proper way to connect myTextView to textContainer?

SAD NOTE: my 30 years of C++ programming does not a smooth transition to Obj-C/Cocoa make...

@implementation DictionaryWindowController

- (id)init {
    self = [super init];
    // This is actually a separate Cocoa window in a Carbon app -- I load it from the NIB upon command from a Carbon menu event...
    NSApplicationLoad();
    if (![NSBundle loadNibNamed:@"Cocoa Test Window.nib" owner:self]) {
        NSLog(@"failed to load nib");
    }

    if (self) {

        // textStorage is a NSTextStorage* in DictionaryWindowController (NSObject)
        textStorage = [[NSTextStorage alloc] initWithString:@""];

        NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
        [givenStorage addLayoutManager:layoutManager];
        [layoutManager autorelease];

        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(kLargeWidthForTextContainer, LargeNumberForText)];
        [layoutManager addTextContainer:textContainer];
        [textContainer autorelease];

        // Is this how to "connect" the myTextView (NSTextView) from the .nib to the textStorage/layoutManager/textContainer?
        [myTextView replaceTextContainer:textContainer];

        [myTextView setMaxSize:NSMakeSize(LargeNumberForText, LargeNumberForText)];
        [myTextView setSelectable:YES];
        [myTextView setEditable:YES];
        [myTextView setRichText:YES];
        [myTextView setImportsGraphics:YES];
        [myTextView setUsesFontPanel:YES];
        [myTextView setUsesRuler:YES];
        [myTextView setAllowsUndo:YES];

        // shouldn't I be able to set the string in the NSTextStorage instance and cause the NSTextView to change its text and redraw?
        [[textStorage mutableString] setString:@"Default text from initialization..."];

    }

    return self;
}



- (IBAction)buttonWasPressed:(id)sender {

    // Pressing the button DOES get to this point, but the NSTextView didn't change...
    [[textStorage mutableString] setString:@"After button press, this text should be the content of the NSTextView."];
}

@end
4

1 回答 1

2

我相信您正在“编辑文本视图背后的文本存储”。请参阅 NSTextStorage 的-edited:range:changeInLength:文档。文档在这里有点含糊,但我相信 -setString: 在 -mutableString 您请求的不会通知文本存储本身的更改(它仅在修改时更新属性运行等)。

调用 -edited:range:changeInLength: 或使用 NSTextView 的 -setString: 方法,如 Mike C. 推荐的那样。

于 2013-05-03T20:53:13.813 回答