2012-11-20 2 views
1

J'ai une application basée sur un document, avec un TextView. Je souhaite ajouter une fonctionnalité ouverte qui l'écrit dans TextView. J'ai le code, mais ça ne marche pas. Le TextView est vide. Heres mon code:Ajouter une fonctionnalité ouverte à l'application basée sur le document

#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 

Merci!

Veuillez ne pas le signaler comme "Pas une vraie question" ou autre chose.

Répondre

2

Le problème est que les méthodes readXXX sont appelées avant la création de la fenêtre. Cela signifie que _textView est nul. Vous devez utiliser -(void)windowControllerDidLoadNib:(NSWindowController *)windowController pour remplir _textView avec les informations que vous chargez à partir du fichier.

Vous pouvez éviter d'être pris par ce genre de problème à l'avenir en plaçant appelle NSAssert dans votre code pour confirmer les conditions préalables nécessaires à vos méthodes pour fonctionner correctement:

NSAssert(_textView != nil, @"_textView not initialized"); 
Questions connexes