我正在尝试学习Swift槽创建一个简单的OSX应用程序。我此刻唯一的目的是在不使用故事板的情况下打开一个窗口。我的项目现在包含3个文件。我自己并没有写大部分内容,我在互联网上找到了它。
运行项目时,没有错误。问题是我创建的窗口根本没有显示出来。
这是我的代码:
AppDelegate.swift :(打印的消息在运行时不会显示...)
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let mainWindowController = MainWindowController()
func applicationDidFinishLaunching(_ aNotification: Notification) {
print("app finished launching")
mainWindowController.showWindow(self)
}
func applicationWillTerminate(_ aNotification: Notification) {
}
}
MainViewController.swift:
import Cocoa
class MainViewController: NSViewController {
override func loadView() {
let rightView = NSView(frame: NSRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 400, height: 400)))
view = rightView
}
override func viewDidLoad() {
let rightView = NSView(frame: NSRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 400, height: 400)))
view = rightView
super.viewDidLoad()
}
}
MainWindowController.swift:
import Cocoa
class MainWindowController: NSWindowController {
convenience init() {
let MVC = MainViewController(nibName: nil, bundle: nil)
self.init(window: NSWindow(contentViewController: MVC!))
window?.titleVisibility = .visible;
let size = NSSize(width: 100, height: 100)
window?.setContentSize(size)
window?.title = "*Random text*"
window?.isMovableByWindowBackground = true
window?.titlebarAppearsTransparent = false
}
override func windowDidLoad() {
super.windowDidLoad()
}
}
有人对我在这里做错了什么有任何建议吗?任何帮助都将深深感激!!