NSTextViewのサンプルです。
Outletを以下の様に定義し、
@interface CustomController : NSObject {
IBOutlet NSTextView *txtView;
}
setString:メソッドでテキストの設定。
[txtView setString:str];
stringメソッドでテキストの取得が行えます。
str = [txtView string];
主な処理は以下の様になります。
- (IBAction)loadButton:(id)sender { NSOpenPanel* openPanel; NSData *data; NSString *str; int result; openPanel = [NSOpenPanel openPanel]; result = [openPanel runModalForTypes:nil]; if (result == NSOKButton) { data = [NSData dataWithContentsOfFile:[openPanel filename]]; str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // 文字コードは UTF-8 前提で読み込む [txtView setString:str]; [str release]; } } - (IBAction)saveButton:(id)sender { NSSavePanel* savePanel; int result; NSString *str; savePanel = [NSSavePanel savePanel]; result = [savePanel runModal]; if (result == NSFileHandlingPanelOKButton) { str = [txtView string]; if ([str writeToFile:[savePanel filename] atomically:YES encoding:NSUTF8StringEncoding error:nil] != YES) { [[NSAlert alertWithMessageText:@"error" defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@"ファイル保存に失敗しました。"] runModal]; } } }
公式の解説はこちら。
NSSavePanel Class Reference
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView/Reference/Reference.html
日本語の解説が良い人はこちらのSatoshi Oomoriさんのページを参照下さい。
http://www.oomori.com/cocoafw/ApplicationKit/NSTextView/index.html
※注記
http://www.oomori.com/cocodesu/index.html