views:

172

answers:

3

I have subclassed NSPersistentDocument. I have renamed the window too. But when I run the application I get the title of the application window as "Untitled". There is no -setTitle: method which I can use to change the title. Any ideas how can I go about doing this?

+2  A: 

Did you set the title by sending setTitle: to the window?

If so, that's wrong. Set the displayName of the document instead. (Remember, NSPersistentDocument is a subclass of NSDocument.)

Peter Hosey
+3  A: 

You don't change the title, your users do by saving documents.

Chris Hanson
A: 

Hi there,

You can bind the Window's title to the document and use Key-Value-Observation to update it.

With Interface Builder select the 'Window' of MyDocument.xib and move over to 'Bindings' tab in the inspector. Check the 'Title' to bind to 'File's Owner' and the 'Model Key Path' to be 'title'.

Then in your subclass of NSPersistentDocument add this code

@interface MyDocument : NSPersistentDocument {
  NSString * _title;
}  
@end

@implementation MyDocument

//P All kinds of all your good stuff here

- (NSString *) title {
  return _title;
}

@end

Now if you want to change to the title of the window you can use KVO. For example

- (BOOL)readFromURL:(NSURL *)absoluteURL 
             ofType:(NSString *)typeName 
              error:(NSError **)outError {

  //P All your good code

  [self willChangeValueForKey:@"title"];
  _title = [absoluteURL lastPathComponent];
  [self didChangeValueForKey:@"title"];  

  //P More good code

}

Hope this helps!

-p.

Robert Daniel Pickard