简体   繁体   English

SwiftUI - 单击 NavigationLink 时触发其他操作

[英]SwiftUI - Trigger other actions when click NavigationLink

I create a list view with a button in an up layer.我在上层创建了一个带有按钮的列表视图。 I want to hide the button immediately right after user clicks the NavigationLink and doesn't see it in a detail view.我想在用户单击 NavigationLink 后立即隐藏按钮并且在详细视图中看不到它。

I implement it successfully by using @State var showAddButton and control it by onDisappear and onAppear action like below, but the button won't disappear if the main view doesn't disappear completely.我通过使用@State var showAddButton成功实现它并通过如下所示的onDisappearonAppear操作控制它,但如果主视图没有完全消失,按钮不会消失。

Does anyone have any other solution to trigger other actions and keep the original link action of NavigationLink?有没有人有任何其他解决方案来触发其他操作并保留 NavigationLink 的原始链接操作?

@State var showAddButton = true

    var body: some View {

        ZStack{

            NavigationView{
                List{
                    ForEach(items, id: \.id){ item in
                        NavigationLink(destination: WorkItemDetailView(item: item)){
                            WorkItemListRow(item: item)
                        }.onDisappear{self.showAddButton = false}
                            .onAppear{self.showAddButton = true}

                    }
                }
                .navigationBarTitle("List", displayMode: .inline)   
            }           

            if showAddButton {
                FloatAddButton()
            }
        }
    }

If you want to stick that button to the main view, you may switch the position of ZStack:如果您想将该按钮粘贴到主视图,您可以切换 ZStack 的 position:

   NavigationView{
        ZStack{
        List{
            ForEach(items, id: \.self){ item in
                NavigationLink(destination: WorkItemDetailView(item: item)){
                    WorkItemListRow(item: item)
                }

            }

        }
        .navigationBarTitle("List", displayMode: .inline)
        Button("mybutton",action: {})}
    }

You can also use something like this你也可以使用这样的东西

struct NavigationBlock<Destination: View, Content: View>: View {
    @State private var isActive: Bool = false
    var destination: Destination
    var content: (Binding<Bool>) -> Content
    init(destination: Destination, @ViewBuilder content: @escaping (Binding<Bool>) -> Content) {
        self.destination = destination
        self.content = content
    }

    var body: some View {
        Group {
            content($isActive)
            NavigationLink(destination: destination,
                           isActive: $isActive) { Spacer() }.hidden()
        }
    }
}

Usage:用法:

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationBlock(destination: Text("Hello")) { binding in
                Button(action: {
                    print("User did tap 'Hello' button")
                    binding.wrappedValue = true
                }) { Text("Hello button") }
            }
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM