返回上一个视图后,SwiftUI NavigationLink 突出显示保持突出显示

问题描述

我在 SwiftUI 中有一系列视图。一种是“菜单视图”,它由包裹在 NavigationView 中的 NavigationLinksList 组成。

代码如下。

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])
           }
        }
     }
}

最后,VoiceList 视图如下:

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)
                }
            }
        }
}

我遇到的问题是,当应用从 VoiceList 视图返回到 设置 视图时,NavigationLink 仍然突出显示,好像它仍然处于活动状态,如附加的屏幕截图所示。老实说,我不知道是什么导致了这种情况。非常感谢任何想法或见解。

enter image description here

解决方法

您可以对 onReceive 使用 List 操作:

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)。

,

我在使用 List 时遇到了同样的问题,为了解决这个问题,我使用:/* works in a circular doubly linked list,where the first item (*list) is linked with the last item of the list */ /* returns the list after the node is removed */ struct node* remove_node(struct node *list,struct node *node) { assert(list != NULL); assert(node != NULL); /* node must be in a list to be removed */ assert(node->before != NULL); assert(node->after != NULL); /* removing head */ if (list == node) { /* move head to next node */ list = list->after; /* still the same node (only one node in list) */ if (list == node) list = NULL; } /* Linking the previous and next node together will effectively remove the node from the list */ node->before->after = node->after; node->after->before = node->before; /* this is not necessary but recommended for error checking */ node->before = NULL; node->after = NULL; return list; } ,但是正如您所料,您将没有选择方面,因此正确的方法是尝试存储选择状态并在离开屏幕之前清洁,但在我的尝试中,我不知道该怎么做。

示例:

UITableViewCell.appearance().selectionStyle = .none

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...