1

How do you implement functions like viewDidLoad() in an empty project without a storyboard?

Sorry for the beginner question. I have been searching for hours now but it still doesn't work. Thanks for any help!

edit:
I am using a .xib file, just no .storyboard. At the moment I'm setting everything up inside applicationDidFinishLaunching()

edit2:
I think I finally got it:
I created a new project without a storyboard, created a file ViewController.swift together with an ViewController.xib and added

window?.contentViewController = ViewController()

into the applicationDidFinishLaunching function inside the AppDelegate. I cannot believe I didn't get this sooner. Sorry BaseZen for your wasted time and thank you very much for trying to help me!

Domenic
  • 11
  • 4
  • Have you read SO-suggested: http://stackoverflow.com/questions/28792722/osx-application-without-storyboard-or-xib-files-using-swift?rq=1 – BaseZen Aug 07 '16 at 15:31
  • Or do you need help loading from a NIB, because that should be easy to search for, e.g.: http://stackoverflow.com/questions/7814928/how-to-load-a-xib-file-in-a-uiview but translated to Swift, e.g. `NSBundle.mainBundle().loadNibNamed(...)` still need more clarity on which stage you're really having trouble with. – BaseZen Aug 07 '16 at 18:39

1 Answers1

1

The question is (far) too broad. Please make it more specific, perhaps tailoring it to the part of it that is answered here, just for the sake of others in the future.

There are in-code equivalents to all Storyboard techniques. Your google technique is:

MacOS Swift how to doXyz programmatically

By the way, iOS is probably 10x more popular in terms of search results. So if the platform doesn't matter and you just want to learn how to do it in code, start with iOS.

This will get you started with layout: https://www.hackingwithswift.com/read/6/3/auto-layout-in-code-addconstraints

For example, initializing a non-IBOutlet:

var myButton = NSButton()

Setting its text:

myButton.title = "Hello"

Adding it to a View Controller's view:

view.addSubview(myButton)

Constraining it within the view:

// You have to learn some verbose weird stuff to do this in code
myButton.translatesAutoresizingMaskIntoConstraints = false
view.addConstraint(NSLayoutConstraint( ... ))

Connecting it to a non-IBAction handler, assuming you have a function called func handleAction(sender: AnyObject)

myButton.target = self
myButton.action = #selector(MyViewController.handleAction(_:))

Simplest possible example:

import Cocoa

class ViewController: NSViewController {
    var b = NSButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        b.title = "Hi! I am a button"
        b.target = self
        b.frame = NSRect(x: 100, y: 100, width: 100, height: 25)
        b.action = #selector(ViewController.handleButtonPress(_:))
        view.addSubview(b)
    }

    func handleButtonPress(sender: NSButton) {
        b.title = "Thank you for clicking."
        b.sizeToFit()
    }
}
BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • Thanks for your response. I'm sorry I didn't make myself clear. I am using a .xib, just no .storyboard. At the moment I'm setting everything up inside applicationDidFinishLaunching() – Domenic Aug 07 '16 at 18:25
  • Sorry for the late reply and thanks for your time! I would love an example of how to implement a view controller in an empty macOS application created without a storyboard. – Domenic Aug 09 '16 at 08:51