applicationDidFinishLaunching not invoked
In your MainMenu.xib, make sure there's an instance of your AppDelegate class. To make one, drag a plain object (blue cube) into the list and set its class name to AppDelegate (or whatever your app delegate class name is).
Also in the MainMenu.xib, to connect it, drag a connection from the Application object to your AppDelegate instance (the blue cube) and connect it to the delegate outlet.
Done.
NSApplicationDelegate's applicationDidFinishLaunching is not being called
If you want to implement Objective-C protocols in Swift 3, you need to use _
indicating the method has no label for the first parameter.
func applicationDidFinishLaunching(_ aNotification: Notification) {
(UPDATE)
Sorry, in my first code, I have forgotten to to replace NSNotification
with Notification
. (Thanks, Leo Dabus.)
Action of StatusItem not working in Swift
If the button is showing up and nothing is happening when you click it, it looks to me like you need to make sure you're setting your button's target
to your App
instance. E.g.:
button.target = self
Otherwise the action is only followed up the responder chain.
applicationDidFinishLaunching not running
Does your Application
object have it's delegate set to your Application App Delegate
object? Right click on Application
in your IB window and set the delegate
outlet to be your Application App Delegate
object.
NSApplicationDelegate not working without Storyboard
You need to do a few things here
- Delete
NSMainStoryboardFile
key/value from the plist - Create a
NSApplication
subclass and assign it to thePrincipal Class (NSPrincipalClass)
key.
The name must be fully qualified with your module name.
- Manually instantiate your delegate in your NSApplication subclass and assign it to the
delegate
property.
Make sure you keep a strong reference to your delegate object. Ive just used a let
here.
class GrookApplication: NSApplication {
let strongDelegate = AppDelegate()
override init() {
super.init()
self.delegate = strongDelegate
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
e.g a simple delegate.
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
override init() {
super.init()
print("wassup")
//conceptual proof of life init override
//wait until applicationDidFinishLaunching , specially for UI
}
var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
print("yo! I'm alive")
window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 200, height: 200), styleMask: .titled, backing: .buffered, defer: false)
window.makeKeyAndOrderFront(nil)
}
}
EDIT 2018 Verified High Sierra
Do NOT try and do window or view controller initialisation inside init
this leads to double app initialisation issues and crashing. The app has not finished launching at this stage. Wait until applicationDidFinishLaunching
to fire any significant operations.
Swift OSX what is the first method to be run?
You have 4 options (in order of appearance):
- Override
init()
- Override
awakeFromNib()
- Delegate notification
applicationWillFinishLaunching
- Delegate notification
applicationDidFinishLaunching
The best place depends on your needs.
- If
IBOutlets
are involved put it not beforeawakeFromNib
. - If you are using a view based table view don't put it in
awakeFromNib
because it could be called multiple times. - Registering
NSUserDefaults
can be put everywhere. - Consider also lazy initialization.
- Consider also Cocoa Bindings.
Cocoa Mac : creating window from AppDelegate
You need to declare your NSWindowController variable main out of applicationDidFinishLaunching method. You need also to call makeKeyAndOrderFront(nil) instead of becomeFirstResponder:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var main: NSWindowController!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
main = NSStoryboard(name : "Main", bundle: nil).instantiateController(withIdentifier: "MainWindow") as! NSWindowController
let mainVc = NSStoryboard(name:"Main", bundle: nil).instantiateController(withIdentifier: "MainViewController") as! ViewController
main.window?.contentViewController = mainVc
main.window?.makeKeyAndOrderFront(nil)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
Related Topics
Kvo Listener Issues in Swift 4
How to Programmatically Change The Xdr Display Reference Mode (Aka Preset) on Macos
Linking Pages in Swift Playgrounds [Xcode]
Invalid Update: Invalid Number of Rows in Section 1
Expressions Are Not Allowed at The Top Level
Changing a Label in Prepareforsegue
Xcode 9.3 Watchkit Crash on Wkinterfacebutton Tap
Convert Ble Current Time to Date
Firebase Data Structure Best Practice for User Interaction
Uibutton Background Color Overlaps Text on Highlight
How to Create Viewcontrollers Without Storyboard and Set One as Delegate of The Other One
Load a Spritekit Scene from Another Bundle
Why Does User Defaults Publisher Trigger Multiple Times
Show Status Bar Only for iPhone X
How to Create Rounded Image with Border and Shadow as Mkannotationview in Swift