OSX应用程序,无需使用Swift的storyboard或xib文件 [英] OSX application without storyboard or xib files using Swift

查看:624
本文介绍了OSX应用程序,无需使用Swift的storyboard或xib文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Unfortunately, I haven't found anything useful on the Internet - I wanted to know, what code I actually have to type for initializing an application without using storyboard or XIB files in Swift. I know I have to have a .swift file called main. But I don't know what to write in there (like do I need autoreleasepool or something like that?). For example, what would I do for initializing an NSMenu and how would I add a NSViewController to the active window (iOS's similar .rootViewController doesn't help). Thanks for any help ;)

编辑:
我实际上不想使用 @NSApplicationMain AppDelegate 前面。

推荐答案

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

if you don't want to have the @NSApplicationMain attribute, do:


  1. 有一个文件main.swift

  2. 级别代码:

  1. have a file main.swift
  2. add following top-level code:

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


在你的应用程序代理。例如:

the rest should be inside your app delegate. 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: NSResizableWindowMask, backing: NSBackingStoreType.Buffered, defer: false)

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

        newWindow!.makeKeyAndOrderFront(nil)
    }
}

那么你有一个viewController

then you have a 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.redColor().CGColor
        self.view = view
    }
}

这篇关于OSX应用程序,无需使用Swift的storyboard或xib文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆