【发布时间】:2021-11-23 16:19:41
【问题描述】:
我正在尝试将 SwiftUI 视图加载为 macOS 应用程序的第一个视图控制器,不幸的是它在 macOS 窗口中没有显示任何内容。以下是我为 macOS 编写应用程序委托的方式。
class AppDelegate: NSObject, NSApplicationDelegate {
let contentView = NSHostingController(rootView: ContentView.init())
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
let storyBoard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: Bundle.main)
let window = storyBoard.instantiateController(withIdentifier: "Main Window") as! NSWindowController
window.contentViewController = contentView
}
}
内容视图
struct ContentView: View {
var body: some View {
Text.init("Hello World!")
}
}
我的目标是我想为 macOS 设置 rootViewController,这个视图控制器是用 SwiftUI 制作的。非常感谢任何帮助。
【问题讨论】:
标签: ios swift macos swiftui appdelegate