Creating a Cocoa application without NIB files

前端 未结 11 735
余生分开走
余生分开走 2020-12-04 10:24

Yes, I know this goes against the whole MVC principle!

However, I\'m just trying to whip up a pretty trivial application - and I\'ve pretty much implemented it. Howe

11条回答
  •  失恋的感觉
    2020-12-04 10:51

    Though this is a few years old question...

    Here's minimal code snippet to bootstrap a Cocoa application in Swift.

    import AppKit
    
    final class ExampleApplicationController: NSObject, NSApplicationDelegate {
        let window1 =   NSWindow()
    
        func applicationDidFinishLaunching(aNotification: NSNotification) {
            window1.setFrame(CGRect(x: 0, y: 0, width: 800, height: 500), display: true)
            window1.makeKeyAndOrderFront(self)
        }
    
        func applicationWillTerminate(aNotification: NSNotification) {
        }
    
    }
    
    autoreleasepool { () -> () in
        let app1        =   NSApplication.sharedApplication()
        let con1        =   ExampleApplicationController()
    
        app1.delegate   =   con1
        app1.run()
    }
    

    Also, I am maintaining a bunch of programmatic examples for Cocoa including bootstrapping, window, menu creations.

    • CocoaProgrammaticHowtoCollection

    See subprojects for desired language.

    • Swift examples
    • Objective-C Examples

提交回复
热议问题