我有一個應用程式,我需要一個按鈕來打開兩個視窗。在 App 結構中,我設定了兩個視窗組
@main
struct myApp: App {
var body : some Scene {
WindowGroup() {
WelcomeView()
}
WindowGroup("FirstViewGroup") {
FirstView()
}
.handlesExternalEvents(matching: Set(arrayLiteral: "FirstViewGroup"))
WindowGroup("SecondViewGroup") {
SecondView()
}
.handlesExternalEvents(matching: Set(arrayLiteral: "SecondViewGroup"))
}
}
在 WelcomeView 我有以下按鈕操作:
Button {
if let firstURL = URL(string: "myApp://FirstViewGroup"), let secondURL = URL(string: "myApp://SecondViewGroup") {
NSWorkspace.shared.open(firstURL)
NSWorkspace.shared.open(secondURL)
}
} label: {
Text("Open Windows")
}
當我注釋掉按鈕操作中的 NSWorkspace.shared.open 行之一時,會打開相應的視窗。但是當我有兩個連續的 NSWorkspace.shared.open 呼叫時,我只得到第一個。想法?我正確設定了 info.plist 中的 URL 型別(因為每個視窗在單獨呼叫時都會成功打開)
uj5u.com熱心網友回復:
嘗試在下一個事件回圈中打開第二個 URL,例如
Button {
if let firstURL = URL(string: "myApp://FirstViewGroup"), let secondURL = URL(string: "myApp://SecondViewGroup") {
NSWorkspace.shared.open(firstURL)
DispatchQueue.main.async {
NSWorkspace.shared.open(secondURL) // << here !!
}
}
} label: {
Text("Open Windows")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452939.html
