SwiftUI - onDeleteCommand 不适用于 macOS 上的 NavigationSplitView

Max*_*Max 4 macos xcode swiftui

我正在开发 macOS 13 应用程序,并且正在使用新的NavigationSplitView. 问题是它不允许我们使用.onDeleteCommand(perform:)(或者也许我用错了)。这是我所做的:

为了使用.onDeleteCommand(perform:),视图需要聚焦。我做了一个简单的应用程序,显示 3 个矩形,我可以使用TAB键进行选择,当我按DELETE键或在菜单栏中的“编辑”>“删除” (两者都会触发.onDeleteCommand)时,它会切换为白色或原始颜色。

VStack {
    Rectangle()
        .fill((isColorDeleted.contains(.blue) ? Color.white : Color.blue))
        .padding()
        .focusable()
        .focused($focusedColor, equals: .blue)

    Rectangle()
        .fill((isColorDeleted.contains(.red) ? Color.white : Color.red))
        .padding()
        .focusable()
        .focused($focusedColor, equals: .red)

    Rectangle()
        .fill((isColorDeleted.contains(.yellow) ? Color.white : Color.yellow))
        .padding()
        .focusable()
        .focused($focusedColor, equals: .yellow)
}
.onDeleteCommand {
    if let focusedColor {
        if !isColorDeleted.contains(focusedColor) {
            isColorDeleted.append(focusedColor)
        } else {
            let idx = isColorDeleted.firstIndex(of: focusedColor)!
            isColorDeleted.remove(at: idx)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

^^^ 这可以正常工作 ^^^

但如果你把它放在NavigationSplitView这样的位置:

NavigationSplitView(columnVisibility: $visibility) {
    List {
        Text("Main page")
    }
} detail: {
    VStack {
        Rectangle()
            .fill((isColorDeleted.contains(.blue) ? Color.white : Color.blue))
            .padding()
            .focusable()
            .focused($focusedColor, equals: .blue)

        Rectangle()
            .fill((isColorDeleted.contains(.red) ? Color.white : Color.red))
            .padding()
            .focusable()
            .focused($focusedColor, equals: .red)

        Rectangle()
            .fill((isColorDeleted.contains(.yellow) ? Color.white : Color.yellow))
            .padding()
            .focusable()
            .focused($focusedColor, equals: .yellow)
    }
    .onDeleteCommand {
        if let focusedColor {
            if !isColorDeleted.contains(focusedColor) {
                isColorDeleted.append(focusedColor)
            } else {
                let idx = isColorDeleted.firstIndex(of: focusedColor)!
                isColorDeleted.remove(at: idx)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果按照我的解释,当矩形聚焦时按DELETE“编辑”>“删除” ,则不会执行任何操作。事实上,“编辑”>“删除”根本不可单击。

Jak*_*rek 5

您需要onDeleteCommand直接在 NavigationSplitView 上使用修饰符,如下所示:

NavigationSplitView(columnVisibility: $visibility) {
    List {
        Text("Main page")
    }
} detail: {
    VStack {
        Rectangle()
            .fill((isColorDeleted.contains(.blue) ? Color.white : Color.blue))
            .padding()
            .focusable()
            .focused($focusedColor, equals: .blue)

        Rectangle()
            .fill((isColorDeleted.contains(.red) ? Color.white : Color.red))
            .padding()
            .focusable()
            .focused($focusedColor, equals: .red)

        Rectangle()
            .fill((isColorDeleted.contains(.yellow) ? Color.white : Color.yellow))
            .padding()
            .focusable()
            .focused($focusedColor, equals: .yellow)
    }
}
.onDeleteCommand {
    ...
}
Run Code Online (Sandbox Code Playgroud)