iOS Development

ios – Dismissing a number of views inside navigation stack

Spread the love


I’ve stacked a number of view through NavigationLink inside a listing. My app code is complicated, so I’ve tried to simplify it as a lot as I may.

To illustrate I’ve a button “Click on Me” in one of many essential view’s (to maintain issues easy, I’ve saved it in ContentView in my pattern code) which when clicked opens a sheet. This sheet has a navigation stack and navigates to three totally different views.

Final view (named as “ThirdListView”) has 2 buttons – Accomplished and Reset. When Accomplished button is tapped, I wish to shut all of the views and dismiss the sheet and get again to Foremost view (Content material View on this case). When Reset button is tapped, I wish to get to the primary view within the sheet.

Right here is how the hierarchy of my code is:

Content material View -> Opens a sheet known as as MediatorView -> Opens FirstListView inside identical sheet -> Opens SecondListView inside identical sheet -> Opens ThirdListView inside identical sheet.

Here’s what I’m making an attempt to realize:

Faucet Reset on ThirdListView, app ought to navigate again to MediatorView
Faucet Accomplished on ThirdListView, app ought to navigate again to ContentView 

Code:

import SwiftUI
import Basis

struct ContentView: View {
    @State var openMediatorView: Bool = false
    var physique: some View {
        VStack {
            Button {
                self.openMediatorView.toggle()
            } label: {
                Textual content("Click on Me").font(.physique.daring())
            }
        }
        .sheet(isPresented: self.$openMediatorView) {
            MediatorView()
        }
    }
}

struct MediatorView: View {
    let firstList: [String] = ["One", "Two", "Three", "Four", "Five"]
    let secondList: [String] = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]
    let thirdList: [String] = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"]
    
    @State var showFirstRow: Bool = false

    var physique: some View {
        NavigationStack {
            Group {
                if firstList.rely > 1 {
                    FirstListView(isHidden: .fixed(true), shortList: firstList, longList: secondList, thirdList: thirdList)
                } else {
                    SecondListView(isHidden: .fixed(true), longList: secondList, thirdList: thirdList).navigationBarBackButtonHidden()
                }
            }
            .interactiveDismissDisabled()
        }
    }
}

struct FirstListView: View {
    init(isHidden: Binding<Bool>, shortList: [String], longList: [String], thirdList: [String]) {
        self.isHidden = isHidden
        self.shortList = shortList
        self.longList = longList
        self.thirdList = thirdList
    }
    
    var isHidden: Binding<Bool>
    var shortList: [String]
    var longList: [String]
    var thirdList: [String]
    
    var physique: some View {
        ScrollViewReader { proxy in
            VStack {
                Checklist(shortList, id: .hashValue) { worth in
                    NavigationLink(vacation spot: {
                        SecondListView(isHidden: .fixed(true), longList: longList, thirdList: thirdList)
                    },
                    label: {
                        HStack {
                            Textual content(worth)
                            Spacer()
                        }
                        .contentShape(Rectangle())
                    })
                }
                .listStyle(.plain)
                .contentShape(Rectangle())
            }
        }
        .navigationTitle("First Checklist")
        .interactiveDismissDisabled()
    }
}

struct SecondListView: View {
    init(isHidden: Binding<Bool>, longList: [String], thirdList: [String]) {
        self.isHidden = isHidden
        self.longList = longList
        self.thirdList = thirdList
    }
    
    var isHidden: Binding<Bool>
    var longList: [String]
    var thirdList: [String]
    
    var physique: some View {
        ScrollViewReader { proxy in
            VStack {
                Checklist(longList, id: .hashValue) { worth in
                    NavigationLink(vacation spot: {
                        ThirdListView(isHidden: .fixed(true), thirdList: thirdList)
                    },
                    label: {
                        HStack {
                            Textual content(worth)
                            Spacer()
                        }
                        .contentShape(Rectangle())
                    })
                }
                .listStyle(.plain)
                .contentShape(Rectangle())
            }
        }
        .navigationTitle("Second Checklist")
        .interactiveDismissDisabled()
    }
}

struct ThirdListView: View {
    @Setting(.presentationMode) personal var presentationMode
    
    init(isHidden: Binding<Bool>, thirdList: [String]) {
        self.isHidden = isHidden
        self.thirdList = thirdList
    }
    
    var isHidden: Binding<Bool>
    var thirdList: [String]
    
    var physique: some View {
        ScrollViewReader { proxy in
            VStack {
                Checklist(thirdList, id: .hashValue) { worth in
                    HStack {
                        Textual content(worth)
                        Spacer()
                    }
                    .contentShape(Rectangle())
                }
                .listStyle(.plain)
                .contentShape(Rectangle())
                
                // TODO: Tapping this could navigate this view again to Mediator View i.e. first view within the sheet.
                Button {
                    isHidden.wrappedValue = false
                    presentationMode.wrappedValue.dismiss()
                } label: {
                    Textual content("Reset to Mediator View").font(.physique.daring())
                }
            }
        }
        .interactiveDismissDisabled()
        .navigationTitle("Third Checklist")
        .toolbar {
            ToolbarItem(placement: .topBarTrailing) {
                // TODO: Tapping this could navigate this view again to Content material View i.e. shut the sheet utterly.
                Button {
                    isHidden.wrappedValue = false
                    presentationMode.wrappedValue.dismiss()
                } label: {
                    Textual content("Accomplished").font(.physique.daring())
                }
            }
        }
    }
}

One thought on “ios – Dismissing a number of views inside navigation stack

  1. Hello Neat post Theres an issue together with your site in internet explorer would check this IE still is the marketplace chief and a large element of other folks will leave out your magnificent writing due to this problem

Leave a Reply

Your email address will not be published. Required fields are marked *