macos - 使用 Swift 从 Nib 创建 View Controller

标签 macos cocoa swift

我正在尝试按照本教程进行操作: http://www.raywenderlich.com/17811/how-to-make-a-simple-mac-app-on-os-x-10-7-tutorial-part-13

但在 Swift 中完成。

尝试执行创建主视图 Controller 步骤我有以下代码:

    import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet var window: NSWindow
    @IBOutlet var vc: MasterViewController?

    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        vc = MasterViewController(nibName: "MasterViewController", bundle: nil)
    }

    func applicationWillTerminate(aNotification: NSNotification?) {
        // Insert code here to tear down your application
    }


}

但是在 MasterViewController 实例化时我得到了错误: 找不到接受提供的参数的“__conversion”的重载

我做错了什么?

最佳答案

我认为这是一个错误,因为文档说 bundle 可以是 nil 但在 swift 中是不允许的,会抛出该错误。

但是您可以将 NSBundle.mainBundle() 作为 bundle 传递并且它可以工作。

引自 NSViewController 文档:

The bundle in which to search for the nib file. If you specify nil, this method looks for the nib file in the main bundle.

关于macos - 使用 Swift 从 Nib 创建 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24203699/

相关文章:

objective-c - 日历 将事件位置存储到数组对象

macos - 使用原子 Electron 的 OSX 桌面应用程序的自定义 url 协议(protocol)

macos - 如何在 Mac OS X 10.6 的 Finder 中禁用 "USB Over Current Notification"弹出窗口?

swift - 在单独的桌面中打开时在当前桌面上打开 Mac OSX 应用程序

ios - 由于获取核心数据对象时内存使用,应用程序卡住并终止

java - 如何在 Swing 应用程序中使用 Java 获取 macOS Mojave 风格的黑暗模式?

macos - 核心数据预取和 KVO 合规性

ios - 这是 NSIndexSet enumerateIndexesUsingBlock 中的错误吗?

swift - 在 iOS 14 的 SwiftUI 中的主应用程序和 WidgetKit 之间共享数据

ivars(属性)的 Swift 命名约定