2016-10-12 56 views
8

我試圖創建一個macOS應用程序沒有故事板在Xcode 8(穩定)在macOS Sierra。但是,我的AppDelegate甚至沒有啓動。下面的代碼我有:NSApplicationDelegate不工作沒有故事板

import Cocoa 

@NSApplicationMain 
class AppDelegate: NSObject, NSApplicationDelegate { 
    var window: NSWindow! 

    override init() { 
     super.init() 
     print("Init") 
    } 

    func applicationDidFinishLaunching(_ aNotification: Notification) { 
     print("Finished launching") 

     // Create a window 
     window = NSWindow() 

     // Add the view controller 
     let viewController = ViewController() 
     window.contentView?.addSubview(viewController.view) 

     // Show the window 
     window.makeKeyAndOrderFront(nil) 
    } 
} 

無論initapplicationDidFinishLaunching(_ aNotification: Notification)被調用。任何幫助將非常感激。

+0

代碼對我來說看起來非常好......沒有理由不應該啓動init或applicationDidFinishLaunching ... –

+0

爲什麼不在創建新項目時取消選擇使用故事板? –

+0

@LeoDabus因爲它仍然給我一個XIB文件。奇怪的是,如果我將XIB/Storyboard文件作爲「主界面」去除,但保留在項目中,AppDelegate仍然被調用。但是,一旦我刪除它,它就不會被調用。 – Zoyt

回答

10

您需要從plist中

  • 創建NSApplication子類,並將其分配給Principal Class (NSPrincipalClass)關鍵在這裏做了幾件事情

    1. 刪除NSMainStoryboardFile鍵/值。

    assign custom class to principal class

    名稱必須與您的模塊名稱完全合格。

    1. 在您的NSApplication子類中手動實例化您的委託並將其分配給delegate屬性。

    請確保您保持對委託對象的強烈引用。我剛在這裏使用了一個let

    class GrookApplication: NSApplication { 
    
        let strongDelegate = AppDelegate() 
    
        override init() { 
         super.init() 
         self.delegate = strongDelegate 
        } 
    
        required init?(coder: NSCoder) { 
         fatalError("init(coder:) has not been implemented") 
        } 
    
    } 
    

    例如一個簡單的委託。

    @NSApplicationMain 
    class AppDelegate: NSObject, NSApplicationDelegate { 
    
        override init() { 
         super.init() 
         print("wassup") 
        } 
    
        func applicationDidFinishLaunching(_ aNotification: Notification) { 
         print("yo! I'm alive") 
        } 
    
    } 
    
  • +0

    你如何保持強烈的參考?我試過這個,所有的工作都正常 - 窗口顯示出來,但是當我試圖將窗口的'styleMask'設置爲'.titled'時,它給了我一個異常,說'斷言在 - [X.XApplication init]失敗,/ Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1504.82.104/AppKit.subproj/NSApplication.m:1778 2017-04-08 13:25:35.761585 + 0100 X [9073:1059806] [General ]未捕獲的異常被提出 2017-04-08 13:25:35.761601 + 0100 X [9073:1059806] [General]創建多個應用程序。 –

    +0

    @TylerDurden強引用是通過使用'var'或'let'完成的。如果您沒有這樣做,那麼弱代表參考會釋放該實例。即'self.delegate = AppDelegate()'不會工作。至於你的其他問題沒有看到你的代碼,我不知道。我懷疑你試圖做這個'[NSApplication sharedApplication] .window'並且它不工作的原因。 –

    0

    如果有人正在尋找Swift版本(基於@WarrenBurtons答案)。

    的AppDelegate

    @NSApplicationMain 
    class AppDelegate: NSObject, NSApplicationDelegate { 
        var window: NSWindow? 
    
        func applicationDidFinishLaunching(_ aNotification: Notification) { 
         // Insert code here to initialize your application 
         window = NSWindow(contentViewController: RootViewController()) 
         window?.makeKeyAndOrderFront(self) 
        } 
    } 
    
    class RootViewController: NSViewController { 
        override func loadView() { 
         self.view = NSView() 
         self.view.frame = NSRect(x: 0, y: 0, width: 600, height: 400) 
        } 
    } 
    

    的NSApplication的子類

    import Cocoa 
    
    class Application: NSApplication { 
        let strongDelegate = AppDelegate() 
    
        override init() { 
         super.init() 
         self.delegate = strongDelegate 
        } 
    
        required init?(coder: NSCoder) { 
         fatalError("init(coder:) has not been implemented") 
        } 
    } 
    

    的Info.plist進入

    <?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
    <plist version="1.0"> 
    <dict> 
        ... 
        <key>NSPrincipalClass</key> 
        <string>$(PRODUCT_MODULE_NAME).Application</string> 
        ... 
    </dict> 
    </plist> 
    

    我也創建了這個要點,那我會繼續達到dat e用於新的Xcode/Swift版本。 https://gist.github.com/florieger/7ac5e7155f6faf18666f92f7d82f6cbc

    編輯:確保刪除Main.storyboard/MainMenu.xib,否則,你可能最終得到兩個Windows UI中的調試器。