ดังนั้นฉันจึงมีแอปพลิเคชัน SwiftUI ในบางครั้งฉันก็สร้าง NSWindow และกำหนด contentView เช่นนี้
// ////////////////////////////////////////////////////////
// Add token window
// ////////////////////////////////////////////////////////
let configurationView = ConfigurationView().environmentObject(store)
configurationWindow = NSWindow(
contentRect: NSRect(x:0, y: 0, width: 480, height: 500),
styleMask: [.titled, .closable, .miniaturizable, .fullSizeContentView],
backing: .buffered, defer: false
)
configurationWindow.center()
configurationWindow.setFrameAutosaveName("BSchauer")
let hostingController = NSHostingController(rootView: configurationView)
configurationWindow.contentViewController = hostingController
configurationWindow.makeKeyAndOrderFront(nil)
configurationWindow.setIsVisible(false)
...
// later on in the code
@objc func toggleConfigurationWindow() {
if self.configurationWindow.isVisible {
self.configurationWindow.setIsVisible(false)
if let button = self.statusBarItem.button {
self.popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
}
} else {
self.configurationWindow.setIsVisible(true)
self.configurationWindow.contentViewController?.view.window?.becomeKey()
}
}
คุณเห็นว่าวิธีที่ฉันโต้ตอบกับหน้าต่างเพื่อนำเสนอให้กับผู้ใช้คือผ่านแฟล็กที่มองเห็นได้ ปัญหาคือเมื่อหน้าต่างแสดงและปิดด้วยปุ่มปิดบนแถบด้านบน หน้าต่างจะถูกยกเลิกการต่อเชื่อม(?) และครั้งต่อไปที่ผู้ใช้พยายามโต้ตอบกับแอปและเปิดหน้าต่างใหม่อีกครั้ง ฉันพบข้อผิดพลาดในการแบ่งส่วน
สิ่งหนึ่งที่ฉันพยายามคือแทนที่จะตั้งค่าการมองเห็นเป็นเท็จ เพียงสร้างหน้าต่างใหม่อีกครั้ง แต่ฉันยังคงได้รับข้อผิดพลาดในการแบ่งส่วน
คำตอบก่อนหน้านี้ทั้งหมดที่ฉันพบกำลังจัดการกับวิธีเก่าในการจัดการกับ NSViewController และการแทนที่วิธี windowShouldClose
แต่ดูเหมือนว่าฉันจะไม่สามารถทำงานได้
TL:DR: เมื่อผู้ใช้กดปุ่มปิดสีแดงบนหน้าต่าง แทนที่จะหน้าต่างถูกทำลาย ฉันแค่ต้องการตั้งค่าการมองเห็นเป็นเท็จ