Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 582
Phil Avatar asked Apr 13 '17 17:04

Phil


People also ask

How do I create an XIB file?

Create a XIB FileRight click on the portion of the screen where your project's files are (view controller, storyboard, etc), and choose new file . Xcode will prompt you for which file type you'd like to create. Choose the View option under the user interface menu.

What's the difference between Storyboard and XIB?

XIB and Storyboard are used for creating interfaces for users. One important point is,xibs are used for creating a single view(it has single file owner at the top of the xib file), but in-case for viewcontroller, multiple screens can be added and its flow can also be monitored(it has separate file owners).

What is an XIB file in Xcode?

What are XIB files? From the point of view of the UI designer, XIB files contain the views or windows that you design in Interface Builder – the controls, their layout and properties, and their connections to your code. It is important to understand that on a technical level, XIB files are stored object graphs.


1 Answers

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

like image 106
rob mayoff Avatar answered Oct 06 '22 22:10

rob mayoff