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

bor*_*ero 28 macos cocoa swift

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

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

Dai*_*jan 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
    
    Run Code Online (Sandbox Code Playgroud)

其余的应该在你的app委托中.例如:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你有一个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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对不起,这不是'为我编写所有代码' - 您的问题已100%回答,您可以提出不同的具体问题或(更好的可能)尝试一些教程 (4认同)
  • 当我尝试使用Xcode 9时,应用终止抱怨无法找到xib文件.我在哪里告诉NSApplication不要使用xib文件? (2认同)

小智 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)
Run Code Online (Sandbox Code Playgroud)

  • 不再适用于Swift 4.我花了几个小时仍然没有运气以编程方式创建一个纯粹的swift窗口.Apple制作是如此难以做到(\阅读没有故事板的官方入门指南) (2认同)

Nos*_*caj 8

在Swift 4中,它再次略有变化,

主文件必须有

import Cocoa

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

NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
Run Code Online (Sandbox Code Playgroud)

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

视图控制器是相同的

  • 对于像我这样的人,希望使窗口显示标题框架`window = NSWindow(contentRect:winSize,styleMask:[.miniaturizable,.closable,.resizable,.titled],后备:.buffered,defer:false)` (2认同)