我正试图使用新的WindowGroup在新窗口中显示更复杂的视图,但不知怎么的,我不知道如何将值传递到视图中
到目前为止,我一直在玩NSWindow,但在那里我无法将新工具栏与.toolbar{}
一起使用,而且在使用最新的swiftUI功能时,我不知何故出现了奇怪的错误
在我的旧代码中,我可以像往常一样将我的值传递到新视图中:
.simultaneousGesture(TapGesture(count: 2).onEnded {
var window: NSWindow!
if nil == window {
let serverView = serverView(content: content) // parse my struct content(name: "XServe-Test", configFile: "/FileUrl", permissions: .rootPermissions, cluster: cluster(x12, CPUMax: .cores(28), ramMax: .gb(1200)))
window = NSWindow(
contentRect: NSRect(x: 20, y: 20, width: 580, height: 400),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered,
defer: false)
window.center()
window.setFrameAutosaveName("ServerMainView")
window.isReleasedWhenClosed = false
window.title = content.name
window.contentView = NSHostingView(rootView: serverView)
window.toolbar = NSToolbar()
window.toolbarStyle = .unifiedCompact
}
window.makeKeyAndOrderFront(nil)
}
现在我在应用程序文件中使用:
import SwiftUI
@main
struct myApp: App {
var body: some Scene {
WindowGroup {
contentView()
}.windowStyle(HiddenTitleBarWindowStyle())
.commands {
SidebarCommands()
ToolbarCommands()
}
// the view that should be displayed in a new window
WindowGroup("serverView") {
let inputContent : content = content(name: "XServe-Test", configFile: "/FileUrl", permissions: .rootPermissions, cluster: cluster(x12, CPUMax: .cores(28), ramMax: .gb(1200)))
serverView(content: inputContent) // this is static now :(
}.handlesExternalEvents(matching: Set(arrayLiteral: "serverView"))
}
和以下代码打开视图:
.simultaneousGesture(TapGesture(count: 2).onEnded {
guard let url = URL(string: "com-code-myApp://serverView") else { return }
NSWorkspace.shared.open(url)
}
如何使用WindowGroup逻辑将点击手势的输入传递到新视图?
我找到了两种方法来解决这个问题。我使用第一个,因为我的应用程序是基于文件的。第二个解决方案是基于伟大的Pulse git回购
在这两种情况下,您都需要在Xcode项目设置中注册一个自定义URL:
Tragets->您的应用程序->信息->URL类型,否则它将不起作用。
第一个解决方案:
import SwiftUI
@main
struct myApp: App {
var body: some Scene {
// 'default' view
WindowGroup { contentView() }
// the view that should open if someone opens your file
WindowGroup("fileView") { fileView() }
.handlesExternalEvents(matching: ["file"]) // does the magic
}
}
struct fileView: View {
var body: some View {
VStack{ /* your view content */}
.onOpenURL(perform: { url in
// get url and read e.g.: your info file
})
}
}
// you can open a file using:
Button("openMyFileinAnExtraWindow"){
let fileUrl = URL(fileURLWithPath: "~/Documents/myFile.yourExtension")
NSWorkspace.shared.open(fileUrl)
}
第二种解决方案:
注意:我创建了这个基于伟大的Pulse git repo的代码片段。
import SwiftUI
@main
struct myApp: App {
var body: some Scene {
// 'default' view
WindowGroup { contentView() }
// the view that should open if someone opens your file
WindowGroup { DetailsView() }
.handlesExternalEvents(matching: Set(arrayLiteral: "newWindow")) // this url must be registerd
}
}
struct DetailsView: View {
var body: some View {
ExternalEvents.open
}
}
public struct ExternalEvents {
/// - warning: Don't use it, it's used internally.
public static var open: AnyView?
}
struct contentView: View {
var body: some View {
VStack {
// create a button that opens a new window
Button("open a new window") {
ExternalEvents.open = AnyView(
newWindow(id: 0, WindowName: "I am a new window!" )
)
guard let url = URL(string: "your-url://newWindow") else { return }
NSWorkspace.shared.open(url)
}
}
}
}
struct newWindow: View {
var id: Int
var WindowName: String
var body: some View{
VStack{
Text(WindowName + String(id))
}
}
}
我不确定这是否是将变量传递到新窗口的最佳方式,但它的效果非常令人信服。我对任何解决方案的方法和想法都很满意。