列表中的 SwiftUI 导航链接 [英] SwiftUI NavigationLink in list

查看:23
本文介绍了列表中的 SwiftUI 导航链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试制作一个包含图像和导航链接的列表.在 iOS 14.1 中一切正常,但在我将 iOS 更新到 14.2 后,出现问题.在列表中,当用户单击大图像时,会弹出一个操作表,而当用户单击 systemImage 时,它​​将触发一个导航链接.但是,当我更新到 iOS 14.2 时,无论我单击什么,都会触发 NavigationLink.有人可以向我解释为什么会发生这种情况以及如何解决吗?

I tried to do a list which have image and a navigation link inside. In iOS 14.1 everything work fine but after I update my iOS to 14.2, something break. In the list while the user click the big image there will be a action sheet pop out, while the user click a systemImage it will trigger a navigation link. However, when I update to iOS 14.2, no matter what I clicked, it will trigger the NavigationLink. Can someone explain to me why will this happened and how to solve?

这是示例代码

struct ContentView : View {
    
    @State var showingActionSheet = false
    @State private var action: Int? = 0
    
    var body: some View {
        NavigationView {
            List{
                VStack(alignment: .leading){
                    HStack{
                        VStack(alignment: .leading){
                            Text("my data")
                            Text("2020/12/12")
                        }
                    }
                    Image("profile")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .onTapGesture(count: 1) {
                            self.showingActionSheet.toggle()
                        }
                    HStack{
                        Image(systemName: "message")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 25)
                            .foregroundColor(.gray)
                            .onTapGesture {
                                self.action = 1
                                print("select comment")
                            }
                        
                        NavigationLink(destination: Text("test"), tag: 1, selection: $action) {
                            EmptyView()
                        }
                    }
                }
                .actionSheet(isPresented: $showingActionSheet) {
                    //action sheet
                    ActionSheet(title: Text("Test"), message: Text("Select a selection"), buttons: [
                        .default(Text("test")) { print("test") },
                        .cancel()
                    ])
                }
                
            }
        }
    }
}

推荐答案

尝试以下操作(禁用导航链接会阻止用户对其进行交互,但会以编程方式激活):

Try the following (disabling navigation link prevents user interaction on it, but programmatically it is activated):

HStack{
    Image(systemName: "message")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .frame(width: 25)
        .foregroundColor(.gray)
        .onTapGesture {
            self.action = 1
            print("select comment")
        }
    NavigationLink(destination: Text("test"), tag: 1, selection: $action) {
        EmptyView()
    }.disabled(true)      // << here !!
}

这篇关于列表中的 SwiftUI 导航链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆