简体   繁体   English

用于在 Mac OS X 的新 window SwiftUI 5.3 中打开视图的按钮

[英]Button to open view in new window SwiftUI 5.3 for Mac OS X

I would like to have a button to open a new window and load a view (for the app's Preferences) in SwiftUI for MacOS but am unsure of the correct way to do it.我想要一个按钮来打开一个新的 window 并在 SwiftUI 中为 MacOS 加载一个视图(用于应用程序的首选项),但我不确定正确的方法。

I tried creating a function and calling it from the button action which works but on closing the new window the app crashes with:我尝试创建一个 function 并从有效的按钮操作中调用它,但是在关闭新的 window 时应用程序崩溃:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x20)线程 1:EXC_BAD_ACCESS(代码=1,地址=0x20)

This is my function:这是我的 function:

func openPreferencesWindow() {
    var preferencesWindow: NSWindow!
    let preferencesView = PreferencesView()
    // Create the preferences window and set content
    preferencesWindow = NSWindow(
        contentRect: NSRect(x: 20, y: 20, width: 480, height: 300),
        styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
        backing: .buffered,
        defer: false)
    preferencesWindow.center()
    preferencesWindow.setFrameAutosaveName("Preferences")
    preferencesWindow.contentView = NSHostingView(rootView: preferencesView)
    preferencesWindow.makeKeyAndOrderFront(nil)
}

And this is my button calling it:这是我的按钮调用它:

Button(action: {
    openPreferencesWindow()
}) {
    Text("Preferences").font(.largeTitle).foregroundColor(.primary)
}

I feel like the window should be constructed in AppDelegate but I'm not sure how I would then call it.我觉得 window 应该在 AppDelegate 中构建,但我不确定我会如何称呼它。

You need to keep reference to created preferences window (like to the main window).您需要保留对创建的首选项 window 的引用(如主窗口)。

Here is possible solution (tested with Xcode 11.4 / macOS 10.15.5)这是可能的解决方案(使用 Xcode 11.4 / macOS 10.15.5 测试)

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {

    var window: NSWindow!
    var preferencesWindow: NSWindow!    // << here

    @objc func openPreferencesWindow() {
        if nil == preferencesWindow {      // create once !!
            let preferencesView = PreferencesView()
            // Create the preferences window and set content
            preferencesWindow = NSWindow(
                contentRect: NSRect(x: 20, y: 20, width: 480, height: 300),
                styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
                backing: .buffered,
                defer: false)
            preferencesWindow.center()
            preferencesWindow.setFrameAutosaveName("Preferences")
            preferencesWindow.isReleasedWhenClosed = false
            preferencesWindow.contentView = NSHostingView(rootView: preferencesView)
        }
        preferencesWindow.makeKeyAndOrderFront(nil)
    }

    // ... other code

and now button would look like现在按钮看起来像

Button(action: {
    NSApp.sendAction(#selector(AppDelegate.openPreferencesWindow), to: nil, from:nil)
}) {
    Text("Preferences").font(.largeTitle).foregroundColor(.primary)
}

backup 备份

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM