Phil
Phil

Reputation: 81

Create a XIB only Cocoa Project in xcode 8.3

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 my project:

  1. Create a new Cocoa Application.
  2. Delete Main.Storyboard
  3. Delete ViewController.swift
  4. Add a new file to the project - a Cocoa Class - subclass of NSWindowController, Also create XIB file checked - named MainWindowController
  5. Under General project info- delete reference to Main for Main Interface
  6. in app delegate: add var mainWindowController: NSWindowController? in applicationDidFinishLaunching add

    let mainWindowController = MainWindowController(windowNibname: "MainWindowController")

    mainWindowController.showWindow(self)

    self.mainWindowController = mainWindowController

  7. in project preferences under the info section add Main nib file base name MainWindowController

This does display my window when I run it however, i get the error:

Failed to connect (window) outlet from (NSApplication) to (NSWindow): missing setter or instance variable

and if I add any controls and connect them i get a similar error stating they couldn't be connected.

Phil

Upvotes: 5

Views: 2774

Answers (1)

rob mayoff
rob mayoff

Reputation: 385600

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:

Xcode 9 Cocoa App template project options

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.

templates containing a main menu bar

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:

    adding an NSObject and setting its class

  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”.

Upvotes: 11

Related Questions