【问题标题】:SwiftUI - onDeleteCommand doesn't work with NavigationSplitView on macOsSwiftUI - onDeleteCommand 不适用于 macO 上的 NavigationSplitView
【发布时间】:2022-12-09 19:29:46
【问题描述】:

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

为了使用.onDeleteCommand(perform:),需要聚焦视图。我做了一个简单的应用程序,显示了 3 个矩形,我可以使用标签键,当我按下删除键或在菜单栏中编辑 > 删除(都触发.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)
        }
    }
}

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

但是如果你像这样把它放在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)
            }
        }
    }
}

如果你按删除或者编辑 > 删除当一个矩形像我解释的那样被聚焦时,它什么也没有。事实上,编辑 > 删除根本不可点击。

【问题讨论】:

  • 你能解释一下它是如何不起作用的吗?
  • 我添加了一些细节,主要是在最后 @malhal stackoverflow.com/posts/74429687/revisions
  • @MaxAuMax 我遇到了同样的问题。我的代码有更多内容,在侧边栏和详细视图中有一个删除。当我打印到控制台时,我看到删除只考虑侧边栏项目上的焦点,并且永远不会触发详细视图......
  • 我目前没有修复程序,它困扰着我,很可能是一个框架错误(在我更改为新导航之前曾经工作过)而且我的代码再简单不过了.onDeleteCommand(perform: { print("Delete command received from DETAIL!") })而且这永远不会被打印出来

标签: xcode macos swiftui


【解决方案1】:

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

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 {
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 2022-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多