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