Ниже представлено упрощенное окно «hello world» Obj-C/Cocoa, которое инициализируется из приложения Carbon. .xib содержит NSWindow, который имеет NSView, содержащий NSButton/NSButtonCell и NSScrollView/NSTextView/NSScroller (s).Невозможно установить текст в NSTextView из .xib
Код компилируется и содержит ссылки без предупреждений. Окно отображается правильно, с обоими объектами (кнопка и текстовое поле). Нажатие кнопки действительно переходит к buttonWasPressed, и я не получаю ошибок в отношении неправильных селекторов в отладчике Xcode.
Но текст в NSTextView не изменился.
Я ДУМАЮ У меня есть подходящая розетка для подключения myTextView. Возможно, использование replaceTextContainer не является правильным способом подключения myTextView к textContainer?
SAD Примечание: мои 30 лет программирования C++ не плавный переход к Obj-C/Cocoa сделать ...
@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
Следует также упомянуть - я строю с помощью Xcode & Interface Builder 3.2.6, 10.6.8. – SMGreenfield
Я бы просто сделал setString на самом NSTextView. –
Майк - ты прав, что setString на самом NSTextView работает. Тем не менее, мне интересно, почему метод NSTextStorage/NSLayoutManager/NSTextContainer выше не делает. – SMGreenfield