我试图使用新的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逻辑将输入从抽头手势传递到新视图?
发布于 2022-02-11 14:32:50
我找到了解决这个问题的两种方法。我使用的是第一个,因为我的应用程序是基于文件的。第二个解决方案是基于伟大的脉搏 git。
在这两种情况下,您都需要在以下Xcode项目设置中注册自定义URL:
Traget -> yourApp -> Info -> 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)
}
第二种解决办法:
注意:我基于伟大的脉搏 git创建了这个代码片段。
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))
}
}
}
我不确定这是否是将变量传递到新窗口的最佳方法,但它的工作非常有说服力。我对任何解决办法和想法都很满意。
https://stackoverflow.com/questions/70560660
复制相似问题