gpt4 book ai didi

ios - 返回上一个 View 后,SwiftUI NavigationLink 突出显示保持突出显示

转载 作者:行者123 更新时间:2023-12-03 23:01:33 29 4
gpt4 key购买 nike

我在 SwiftUI 中有一系列 View 。一个是“菜单 View ”,它由 组成。列表导航链接 包裹在 导航 View 。
代码如下。

var body: some View {
NavigationView {
List {
HStack {
NavigationLink(destination: History(), isActive: $isHistoryViewActive) {
Image(systemName: "clock")
Text("History")
}
}

HStack {
NavigationLink(destination: Settings(), isActive: $isSettingsActive) {
Image(systemName: "gear")
Text("Settings")
}
}

HStack {
Image(systemName: "info.circle.fill")
Button(action: {
...
}) {
Text("My Button")
}
}
}
}
}
设置 观点如下
var body: some View {
List {
...
Section(header: "Background Music") {
Toggle("Play", isOn: $isBackGroundMusicOn)
}

Section(header: "Voice Setting") {
HStack {
NavigationLink(destination: VoiceList() {
Text(self.voiceNames[self.selectedVoice])
}
}
}
}
最后, 语音列表观点如下:
var body: some View {
List {
ForEach(0 ..< VoiceList.voiceNames.count) {voiceIndex in
HStack {
Button(action: {
voiceChanged(selectedVoice: voiceIndex)
}){
Text(VoiceList.voiceNames[voiceIndex])
}
Spacer()
Image(systemName: "checkmark")
.frame(alignment: .trailing)
.foregroundColor(.blue)
.isHidden(hidden: voiceIndex != selectedVoice)
}
}
}
}
我遇到的问题是当应用程序从 返回时语音列表查看到 设置 查看, 导航链接保持突出显示,就好像它仍然处于事件状态,如附加的屏幕截图所示。老实说,我不知道是什么导致了这种情况。任何想法或见解都非常感谢。
enter image description here

最佳答案

您可以使用 onReceiveList 的操作:

List {

}.onReceive(NotificationCenter.default.publisher(for: UITableView.selectionDidChangeNotification)) {
guard let tableView = $0.object as? UITableView,
let selectedRow = tableView.indexPathForSelectedRow else { return }

tableView.deselectRow(at: selectedRow, animated: true)
}
这将取消选择所选行。
此解决方法的最初想法归功于 Pivaisan(来自此 Apple Developer thread)。

关于ios - 返回上一个 View 后,SwiftUI NavigationLink 突出显示保持突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65411477/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com