简体   繁体   English

使用 isActive 的 List 中的 NavigationLink 推送错误的行

[英]NavigationLink in List using isActive pushes the wrong row

I'm trying to use NavigationLink's isActive variable to pop back to the root view controller.我正在尝试使用 NavigationLink 的 isActive 变量弹回根视图 controller。

The problem I'm experiencing is that using isActive pushes the wrong row when clicking on a list item.我遇到的问题是单击列表项时使用 isActive 会推送错误的行。 Remove the isActive variable and everything works as expected.删除 isActive 变量,一切都按预期工作。

在此处输入图像描述

Here's some example code for demonstration purposes:下面是一些用于演示目的的示例代码:

struct ContentView: View {
    
    @State private var activateNavigationLink: Bool = false

    var exampleData = ["a", "b", "c"]
    
    var body: some View {
        
        NavigationView {
            
            List(exampleData, id: \.self) { item in
                
                NavigationLink(
                    destination: SecondView(item: item), isActive: $activateNavigationLink) {
                    
                    Text(item)
                }
            }
        }
    }
}

SecondView第二视图

struct SecondView: View {
    
    var item: String
    
    var body: some View {
        Text(item)
    }
}

This is driving me nuts.这让我发疯了。 Any help would be greatly appreciated.任何帮助将不胜感激。

Because activateNavigationLink is just a Bool in your code, if it is true , every NavigationLink will register as active in your List .因为activateNavigationLink在你的代码中只是一个Bool ,如果它是true ,每个NavigationLink都会在你的List中注册为活动的。 Right now, this is manifesting as the last item ( C ) getting pushed each time.现在,这表现为每次推送的最后一项( C )。

Instead, you'd need some system to store which item is active and then translate that to a boolean binding for the NavigationLink to use.相反,您需要一些系统来存储哪个项目处于活动状态,然后将其转换为 boolean 绑定以供NavigationLink使用。

Here's one possible solution:这是一种可能的解决方案:

struct ContentView: View {
    
    @State private var activeNavigationLink: String? = nil

    var exampleData = ["a", "b", "c"]
    
    func bindingForItem(item: String) -> Binding<Bool> {
        .init {
            activeNavigationLink == item
        } set: { newValue in
            activeNavigationLink = newValue ? item : nil
        }
    }
    
    var body: some View {
        
        NavigationView {
            
            List(exampleData, id: \.self) { item in
                
                NavigationLink(
                    destination: SecondView(item: item), isActive: bindingForItem(item: item)) {
                    
                    Text(item)
                }
            }
        }
    }
}

You should not use activeNavigationLink on main view it should be used with cellView您不应该在主视图上使用 activeNavigationLink 它应该与 cellView 一起使用

struct ContentView: View {
        
    var exampleData = ["a", "b", "c"]
    
    var body: some View {
        
        NavigationView {
            
            List(exampleData, id: \.self) { item in
                
               CellView(item: item)
            }
        }
    }
}

CellView单元视图

struct CellView: View {
        
    @State private var activateNavigationLink: Bool = false
        var item: String
        
        var body: some View {
              NavigationLink(
                        destination: SecondView(item: item), isActive: $activateNavigationLink) {
                        
                        Text(item)
                    }
        }
    }

SecondView第二视图

struct SecondView: View {
    
    var item: String
    
    var body: some View {
        Text(item)
    }
}

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

相关问题 列表中的 SwiftUI NavigationLink 没有使用 isActive 获得正确的详细信息页面 - SwiftUI NavigationLink in the list doesn't get the right detail page with isActive 在 SwiftUI 中使用 List 时如何使用 NavigationLink isActive 绑定? - How does one use NavigationLink isActive binding when working with List in SwiftUI? 未启用 isActive 的 NavigationLink 打开目的地 - NavigationLink open destination without isActive enabled NavigationLink、isActive 在 SwiftUI 中不起作用 - NavigationLink, isActive doesn´t work in SwiftUI NavigationLink 推送两次,然后弹出一次 - NavigationLink pushes twice, then pops once NavigationLink isActive在navigationBarItems(trailing :)修饰符内不起作用 - NavigationLink isActive does not work inside navigationBarItems(trailing:) modifier 有没有办法在 SwiftUI 的 NavigationLink 内的 isActive 参数中提供布尔语句? - Is there a way to supply a boolean statement in the isActive parameter within NavigationLink in SwiftUI? 带 isActive 的 NavigationLink 在 macOS SwiftUI 应用程序中创建异常滚动行为 - NavigationLink with isActive creates unusual scrolling behavior in macOS SwiftUI app NavigationLink.navigationDestination 多次调用并推送到新视图两次 - NavigationLink .navigationDestination called multiple times and pushes to new View twice SwiftUI:列表、NavigationLink 和徽章 - SwiftUI: List, NavigationLink, and badges
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM