Converting a basic Cocoa application to a document-based application

后端 未结 4 1658
萌比男神i
萌比男神i 2021-02-07 18:51

My team and I have been working on an existing, non-document-based Cocoa application. This is our first Cocoa app, although we\'ve done a number of iOS apps thus far.

Th

4条回答
  •  别那么骄傲
    2021-02-07 19:49

    An often found suggestion is to create a new document based application and move all you existing code in there. This can be cumbersome for a large workspace with all kinds of stuff nicely configured. Let alone breaking version control.

    I took the following simple steps and it worked:

    • Generate a document based application
    • from this this generated project, copy the following section from the Info.plist (open the file with a normal text-editor):

      CFBundleDocumentTypes
      
          
              CFBundleTypeExtensions
              
                  mydoc
              
              CFBundleTypeIconFile
              
              CFBundleTypeName
              DocumentType
              CFBundleTypeOSTypes
              
                  ????
              
              CFBundleTypeRole
              Editor
              NSDocumentClass
              $(PRODUCT_MODULE_NAME).Document
          
      
      

      and paste it in the Info.plist file in your own project.

    • Copy Document.swift from the generated document-based project into your own project.

    • it contains a method:

      override func makeWindowControllers() {
          // Returns the Storyboard that contains your Document window.
          let storyboard = NSStoryboard(name: "Main", bundle: nil)
          let windowController = storyboard.instantiateController(withIdentifier: "Document Window Controller") as! NSWindowController
          self.addWindowController(windowController)
      }
      

      It creates a new window just the way you application normally would. If you storyboard has only one windowcontroller the 'withIDentifier'-field can contain something arbitrary. If you have more window controllers in your storyboard, the identifier needs to correspond to the right windowcontroller for new documents.

提交回复
热议问题