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
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:
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:
Create a new project using the “Cocoa Application” template.
Delete “Main.storyboard” and “ViewController.swift” (or “ViewController.h” and “ViewController.m”).
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).
In your target's General settings tab, change the Main Interface setting from “Main” to “MainMenu.xib”.
In “MainMenu.xib”, add an NSObject to the document outline. Set its custom class to “AppDelegate”. Demo:
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.
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.
In “MainWindowController.swift”, add overrides for windowNibName
and owner
:
class MainWindowController: NSWindowController {
override var windowNibName: String? { return "MainWindowController" }
override var owner: AnyObject { return self }
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”.