2010-02-22 60 views
0

基于文档的QTKit应用程序。保存时,新文件名在活动窗口titleBar中更新。我还想显示新保存的文件名字符串在textField中,在打开的文档中的其他位置。该代码成功保存了新的文档。但lastPathComponent字符串不会更新。请指教?保存后设置lastPathComponent字符串

感谢,

保罗

- (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo 
{ 
    NSURL *outputFileURL = [(NSURL *)contextInfo autorelease];  



if (returnCode == NSOKButton) { 
    NSString *filename = [sheet filename]; 

    [[NSFileManager defaultManager] moveItemAtPath:[outputFileURL path] toPath:filename error:nil]; 

    NSString *path = [filename lastPathComponent]; 
    [textField setStringValue:[path lastPathComponent]]; 

    [[NSWorkspace sharedWorkspace] openFile:filename]; 
} 


else { 
    [[NSFileManager defaultManager] removeItemAtPath:[outputFileURL path] error:nil]; 


} 

} 
+0

你为什么要实现自救?通常,文档系统会为您处理保存。如果您想要改变它的工作方式,请查看文档控制器的方法,并查看哪些最适合您进行覆盖。 http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSDocumentController_Class/ – 2010-02-23 16:46:44

回答

0

由于“文件名”显然是有效的(因为一切正常,您的窗口标题更新),让你检查,以确保“文本框”实际上是连接你的XIB?

+0

回答你的问题,我又看了一眼。 “path lastPathComponent”似乎是可疑的,因为“path”已经是filename的lastPathComponent。也许只是将“路径”传递给文本视图。更多的描述性变量名称和类似OCD的清洁有助于避免混淆这样的事情。 :-) – 2010-02-22 22:10:36