没有使用Swift的storyboard或xib文件的OSX应用程序

时间:2015-03-01 10:54:22

标签: macos cocoa swift

不幸的是,我没有在互联网上找到任何有用的东西 - 我想知道,我实际需要输入什么代码来初始化应用程序而不使用Swift中的storyboard或XIB文件。我知道我必须有一个名为.swift的{​​{1}}文件。但我不知道在那里写什么(就像我需要autoreleasepool或类似的东西?)。例如,我将如何初始化main以及如何将NSMenu添加到活动窗口(iOS的类似NSViewController无效)。谢谢你的帮助;)

编辑: 我实际上不想在.rootViewController前面使用@NSApplicationMain。我宁愿知道那里到底发生了什么,然后自己做。

4 个答案:

答案 0 :(得分:26)

如果您不想拥有@NSApplicationMain属性,请执行:

  1. 有一个文件main.swift
  2. 添加以下顶级代码:

    import Cocoa
    
    let delegate = AppDelegate() //alloc main app's delegate class
    NSApplication.sharedApplication().delegate = delegate //set as app's delegate
    
    // Old versions:
    // NSApplicationMain(C_ARGC, C_ARGV)
    NSApplicationMain(Process.argc, Process.unsafeArgv);  //start of run loop
    
  3. 其余的应该在您的app委托中。 e.g:

    import Cocoa
    
    class AppDelegate: NSObject, NSApplicationDelegate {
        var newWindow: NSWindow?
        var controller: ViewController?
    
        func applicationDidFinishLaunching(aNotification: NSNotification) {
            newWindow = NSWindow(contentRect: NSMakeRect(10, 10, 300, 300), styleMask: .resizable, backing: .buffered, defer: false)
    
            controller = ViewController()
            let content = newWindow!.contentView! as NSView
            let view = controller!.view
            content.addSubview(view)
    
            newWindow!.makeKeyAndOrderFront(nil)
        }
    }
    

    然后你有一个viewController

    import Cocoa
    
    class ViewController : NSViewController {
        override func loadView() {
            let view = NSView(frame: NSMakeRect(0,0,100,100))
            view.wantsLayer = true
            view.layer?.borderWidth = 2
            view.layer?.borderColor = NSColor.red.cgColor
            self.view = view
        }
    }
    

答案 1 :(得分:17)

上面的顶级代码示例不再适用于最新版本的Xcode。而是使用它:

import Cocoa

let delegate = AppDelegate() //alloc main app's delegate class
NSApplication.shared().delegate = delegate //set as app's delegate

let ret = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

答案 2 :(得分:6)

在Swift 4中它又稍微改变了,

主文件必须

import Cocoa

let delegate = AppDelegate()
NSApplication.shared.delegate = delegate

NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

AppDelegate必须

import Cocoa


class AppDelegate: NSObject, NSApplicationDelegate {
    var newWindow: NSWindow?
    var controller: ViewController?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        newWindow = NSWindow(contentRect: NSMakeRect(10, 10, 300, 300), styleMask: .resizable, backing: .buffered, defer: false)

        controller = ViewController()
        let content = newWindow!.contentView! as NSView
        let view = controller!.view
        content.addSubview(view)

        newWindow!.makeKeyAndOrderFront(nil)
    }
}

视图控制器是相同的

答案 3 :(得分:1)

Xcode 11 / iOS13-情况已更改

简而言之:

  • 您必须删除info.plist中的两行,并在新类 SceneDelegate
  • 中实例化该窗口

详细信息:

在Info.plist中删除此行...

enter image description here

…和这一行(不太容易找到):

enter image description here

在新的 SceneDelegate 中,发生了一种新的魔术,用于初始化我们自己的UIWindow实例。

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    // we try to cast the scene (UIScene) as UIWindowScene
    guard let windowScene = (scene as? UIWindowScene) else { return }

    // We create our window ourselfes and assign this windowScene to its property 'windowScene'
    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    window?.windowScene = windowScene

    // and then as always assign any rootViewController
    window?.rootViewController = WelcomeVC()

    // and make the window key and visible
    window?.makeKeyAndVisible()
}

}