1

TextView を使用したドキュメント ベースのアプリケーションがあります。TextViewに書き込むオープン機能を追加したい。私はコードを持っていますが、うまくいきません。TextView は空です。私のコードは次のとおりです。

    #import "Document.h"

@implementation Document

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSFont *courier = [NSFont fontWithName: @"Courier" size:12];
    [_textView setString: @"Blabla"];
    [_textView setFont:courier];
    NSLog(@"Tesg");
    [_textView setString:@"TEST"];


}

- (id)init
{
        NSLog(@"Tesg");

    self = [super init];
    if (self) {

        // Add your subclass-specific initialization here.



    }
    return self;
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"Document";
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

+ (BOOL)autosavesInPlace
{
    return YES;
}

/*- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    // Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
    // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
    NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return nil;
}
*/
- (NSData *)dataOfType:(NSString *)pTypeName error:(NSError **)pOutError {

    NSDictionary * zDict;

    if ([pTypeName compare:@"public.plain-text"] == NSOrderedSame ) {
        zDict = [NSDictionary dictionaryWithObjectsAndKeys:
                 NSPlainTextDocumentType,
                 NSDocumentTypeDocumentAttribute,nil];
    } else {
        NSLog(@"ERROR: dataOfType pTypeName=%@",pTypeName);
        *pOutError = [NSError errorWithDomain:NSOSStatusErrorDomain
                                         code:unimpErr
                                     userInfo:NULL];
        return NULL;
    } // end if

    NSString * zString = [[_textView textStorage] string];
    NSData * zData = [zString dataUsingEncoding:NSASCIIStringEncoding];
    return zData;

} // end dataOfType
/*
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    // Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
    // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
    // If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
    NSLog(data);
    NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return YES;
}
*/

- (BOOL)readFromData:(NSData *)pData
              ofType:(NSString *)pTypeName
               error:(NSError **)pOutError {

    if ([pTypeName compare:@"public.plain-text"] != NSOrderedSame) {
        NSLog(@"** ERROR ** readFromData pTypeName=%@",pTypeName);
        *pOutError = [NSError errorWithDomain:NSOSStatusErrorDomain
                                         code:unimpErr
                                     userInfo:NULL];
        return NO;
    } // end if

    NSDictionary *zDict = [NSDictionary dictionaryWithObjectsAndKeys:
                           NSPlainTextDocumentType,
                           NSDocumentTypeDocumentAttribute,
                           nil];
    NSDictionary *zDictDocAttributes;
    NSError *zError = nil;
    zNSAttributedStringObj =
    [[NSAttributedString alloc]initWithData:pData
                                    options:zDict
                         documentAttributes:&zDictDocAttributes
                                      error:&zError];
    if ( zError != NULL ) {
        NSLog(@"Error readFromData: %@",[zError localizedDescription]);
        return NO;
    } // end if
    NSString *content = [zNSAttributedStringObj string];
    NSLog(@"%@", content);
    NSLog(@"%c", [_textView isEditable]);
    [_textView setString:content];


    return YES;

} // end readFromData


@end

ありがとう!

「本当の質問ではない」などのフラグを立てないでください。

4

1 に答える 1

2

問題は、ウィンドウが作成される前にreadXXXメソッドが呼び出されることです。これは、それ_textViewがゼロであることを意味します。ファイルからロードする情報-(void)windowControllerDidLoadNib:(NSWindowController *)windowControllerを入力するには、を使用する必要があります。_textView

NSAssert呼び出しをコードに配置して、メソッドが正しく動作するために必要な前提条件を確認することで、将来この種の問題に巻き込まれるのを防ぐことができます。

NSAssert(_textView != nil, @"_textView not initialized");
于 2012-11-20T21:54:21.043 に答える