Create a XIB only Cocoa Project in xcode 8.3

前端 未结 1 594
余生分开走
余生分开走 2021-01-05 21:35

I\'m trying to create a XIB based application in XCode 8.3 but the option to Start a project without a Storyboard has been removed. Here are the steps I am taking to set up

相关标签:
1条回答
  • 2021-01-05 22:03

    UPDATE for Xcode 9 and later

    In Xcode 9.0 and later (through at least 9.4 beta 1), you can once again configure the Cocoa App project template to have a MainMenu.xib instead of a Main.storyboard. Just make sure the “Use Storyboards” option is not checked when choosing the options for your new project:

    ORIGINAL

    Your “Main Interface” setting should point to a storyboard or XIB that contains the application's main menu bar. You can create a XIB containing a main menu bar using either the “Application” file template or the “Main Menu” file template.

    Here are the steps to create a project from scratch that has the main menu bar in one XIB file, and the window in a separate XIB file:

    1. Create a new project using the “Cocoa Application” template.

    2. Delete “Main.storyboard” and “ViewController.swift” (or “ViewController.h” and “ViewController.m”).

    3. Create a new file (File > New File…) using the “Main Menu” template in the User Interface section. Name it “MainMenu” (Xcode will add the “.xib” extension automatically).

    4. In your target's General settings tab, change the Main Interface setting from “Main” to “MainMenu.xib”.

    5. In “MainMenu.xib”, add an NSObject to the document outline. Set its custom class to “AppDelegate”. Demo:

    6. In “MainMenu.xib”, connect the “delegate” outlet of “File's Owner” (which should have class “NSApplication”) to the “App Delegate” object. To do this, hold the control key and drag from “File's Owner” to “App Delegate” in the document outline. Then pick “delegate” from the pop-up list.

    7. Create a new file using the “Cocoa Class” template. Set the class name to “MainWindowController” and set the subclass name to “NSWindowController”. Make sure “Also create XIB file for user interface” is checked.

    8. In “MainWindowController.swift”, add overrides for windowNibName and owner:

      class MainWindowController: NSWindowController {
      
          override var windowNibName: String? { return "MainWindowController" }
          override var owner: AnyObject { return self }
      
    9. In “AppDelegate.swift”, add code to create and show the window of a MainWindowController:

      class AppDelegate: NSObject, NSApplicationDelegate {
      
          func applicationDidFinishLaunching(_ aNotification: Notification) {
              mainWindowController = MainWindowController()
              mainWindowController!.showWindow(nil)
          }
      
          var mainWindowController: MainWindowController?
      

    You may now create additional objects in “MainWindowController.xib” and connect them to outlets and actions in “MainWindowController.swift”.

    0 讨论(0)
提交回复
热议问题