Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

swiftui - Unable to pop to root view after clicking NavigationLink in the row of a List

I'm somewhat new to SwiftUI and I'm struggling to successfully pop to the root controller when clicking on a NavigationLink in a row of a list. I'm following the approach demonstrated in this other post. I'm able to get this implementation working elsewhere in my project, but its a little tricky when a List is involved.

The setup is: ListView contains List and RowView. Clicking on the NavigationLink in RowView segues to View 2. Whats happening is that when I click on the NavigationLink it segues X times if there are X rows in the list. I'm assuming that by clicking on the NavigationLink, I am toggling it the binding bool to true, and as a result all NavigationLinks are being activated.

 struct RowView : View {
    
    @Binding var rootIsActive : Bool
    var body: some View {
        VStack() {
            let view2 = View2(shouldPopToRootView: $rootIsActive)
            HStack() {
                NavigationLink(destination: view2, isActive: $rootIsActive, label: {Text("Go to view 2")}).isDetailLink(false)
                    .background(Color.purple)
                // Additional UI stuff
            }
            .frame(height:50)
        }
    }
}

 struct ListView: View {

    @State var isActive : Bool = false
    var someItems: [SomeObj] = []

    var body: some View {
        NavigationView {
            VStack() {
                // Other UI stuff
                List {
                    ForEach(self.someItems) { item in
                        Section(header: RowView(rootIsActive: $isActive).onTapGesture {
                            // debug code
                        })
                    }
                }
            }
        }
    }
}

struct View2: View {
        
    @Binding var shouldPopToRootView : Bool
    
    var body: some View {
        ZStack() {
            //UI Stuff
        }
        .navigationBarItems(trailing: Button(action: {
            // Pop to root
            self.shouldPopToRootView = false
        }, label: {
            Text("Save")
        }))
    }
}
question from:https://stackoverflow.com/questions/65557558/unable-to-pop-to-root-view-after-clicking-navigationlink-in-the-row-of-a-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...