Pedro Paulo Amorim
Pedro Paulo Amorim

Reputation: 1949

How to load NSViewController on a NSApplicationDelegate

I'm trying to load a ViewController on my appDelegate class. Like this:

class AppDelegate: NSObject, NSApplicationDelegate {

  @IBOutlet weak var window: NSWindow!

  var viewController = HomeViewController()

  func applicationDidFinishLaunching(aNotification: NSNotification) {
     self.window.backgroundColor = NSColor(rgba: "#02303A")
     self.viewController.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame))
     self.window.contentView?.addSubview(self.viewController.view)
  }

}

My view controller is very simples, just:

import PureLayout

class HomeViewController : NSViewController {

  let search : NSSearchField = {
    let search = NSSearchField.newAutoLayoutView()
    return search
  }()

  override func loadView() {
    super.loadView()
    self.view.layer?.backgroundColor = NSColor.blackColor().CGColor
    self.view.addSubview(search)
  }

}

But when the I compile the project, the log returns:

Error

The repository with the complete source code: Github

Anyone know what can be? I do this on my iOS projects and it works fine.

Upvotes: 2

Views: 986

Answers (1)

ElmerCat
ElmerCat

Reputation: 3155

  1. You have no xib file belonging to your view controller, so the first step is to add a new file for the view:

enter image description here

  1. In your newly created xib file, select the File's Owner placeholder, and in the Inspector, set its custom class to your view controller subclass:

enter image description here

  1. Finally, connect the view outlet from the view controller (the File's Owner) to the Custom View object.

enter image description here

(Build your user interface for the view controller in this view, rather than the one in your main nib.)

Upvotes: 2

Related Questions