【问题标题】:Let the user choose what type of document to open让用户选择打开什么类型的文档
【发布时间】:2010-03-22 15:06:29
【问题描述】:

我正在创建一个具有两种文档类型的 NSDocument 应用程序:网站和 Web 服务。这是在我的 Info.plist 中:

<key>CFBundleDocumentTypes</key>
 <array>
  <dict>
   <key>CFBundleTypeName</key>
   <string>Website</string>
   <key>CFBundleTypeExtensions</key>
   <array>
    <string>website</string>
   </array>
   <key>LSTypeIsPackage</key>
   <true/>
   <key>CFBundleTypeRole</key>
   <string>Editor</string>
   <key>LSHandlerRank</key>
   <string>Default</string>
   <key>NSDocumentClass</key>
   <string>AWWebSite</string>
  </dict>
  <dict>
   <key>CFBundleTypeName</key>
   <string>Web Service</string>
   <key>CFBundleTypeExtensions</key>
   <array>
    <string>webservice</string>
   </array>
   <key>LSTypeIsPackage</key>
   <true/>
   <key>CFBundleTypeRole</key>
   <string>Editor</string>
   <key>LSHandlerRank</key>
   <string>Default</string>
   <key>NSDocumentClass</key>
   <string>AWWebService</string>
  </dict>
 </array>

现在,每当用户打开应用程序,从菜单栏中选择“新建”项,或在没有打开的窗口时单击 Dock 图标时,我想显示一个包含两个选项的窗口,每个选项对应一个文档类型。谁能帮我这个?谢谢

【问题讨论】:

标签: objective-c cocoa usability nsdocument


【解决方案1】:

您需要做的是覆盖- [NSDocumentController newDocument:]NSDocumentController 是响应者链的一部分,是最终处理它发送的 newDocument: 消息的对象。

从那里,您可以显示您喜欢的任何对话框,然后调用makeUntitledDocumentOfType:error:addDocument:makeWindowControllersshowWindows。这就是openUntitledDocumentAndDisplay:error: 所做的。

但问题是 NSDocumentController 是一个单例,所以你需要确保实例化的是你的子类,而不是 Apple 的。通常,您通过将子类的对象添加到 MainMenu.xib 或首先加载的任何 NIB 来执行此操作。这通常足以确保您的子类首先被创建并成为单例。

【讨论】:

  • 非常感谢!我得到这个菜单项的工作,但不是 Dock 图标和应用程序启动 =[
  • 您可以改写openUntitledDocumentAndDisplay:error:;我相信这就是您单击停靠图标和应用启动时所调用的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多