HomeiOS Developmentios - NavigationStack Path surprisingly popping on a number of Cut up...

ios – NavigationStack Path surprisingly popping on a number of Cut up View modifications


This easy instance helps a facet bar and tab view pending the dimensions class. The state persists based mostly on my app’s APpNavigation enum.

Nevertheless, there’s a ListsView with two lists (resulting in element views). I would like the element view of the record to persist between altering the iPad’s Cut up View to Tab/Sidebar, it does persist a pair instances, then loses the trail.

App

import SwiftUI

@major
struct splittyApp: App {
    @State personal var mode: AppNavigation? = .house
    @StateObject personal var listsViewModel = ListsViewModel()

    var physique: some Scene {
        WindowGroup {
            ContentView(navigation: $mode)
                .environmentObject(listsViewModel)
        }
    }
}

ContentView

struct ContentView: View {
    @Surroundings(.horizontalSizeClass) personal var horizontalSizeClass
    @Binding var navigation: AppNavigation?
    
    var physique: some View {
        if horizontalSizeClass == .common {
            AppSidebarView(mode: $navigation)
        } else {
            AppTabView(mode: $navigation)
        }
    }
}

AppSidebarView

/// The view displaying a sidebar-based interface.
struct AppSidebarView: View {
    @Binding var mode: AppNavigation?
    
    var physique: some View {
        NavigationSplitView {
            
            // Choice solely works with an non-compulsory.
            Listing(choice: $mode) {
                NavigationLink(worth: AppNavigation.house) {
                    Label("Residence", systemImage: "home")
                }
                
                NavigationLink(worth: AppNavigation.lists) {
                    Label("Lists", systemImage: "record.clipboard")
                }
            }
        } element: {
            change mode {
            case .house: HomeView()
            case .lists: ListsView()
            case .none: Textual content("Error")
            }
        }
    }
}

AppTabView

/// The view displaying a tab-based interface.
struct AppTabView: View {
    @Binding var mode: AppNavigation?
    
    var physique: some View {
        // TabView's choice solely works with non-optional?
        let binding = Binding<AppNavigation> {
            mode ?? .house
        } set: {
            mode = $0
        }

        TabView(choice: binding) {
            HomeView()
                .tabItem {
                    Textual content("Residence")
                }
                .tag(AppNavigation.house)
            
            ListsView()
                .tabItem {
                    Textual content("Lists")
                }
                .tag(AppNavigation.lists)
        }
    }
}

*ListsView


import SwiftUI

struct ListsView: View {
    @EnvironmentObject var viewModel: ListsViewModel
    
    var physique: some View {
        NavigationStack(path: $viewModel.record) {
            Listing {
                NavigationLink("Listing 1", worth: ListsNavigation.list1)
                NavigationLink("Listing 2", worth: ListsNavigation.list2)
            }
            .navigationDestination(for: ListsNavigation.self) { worth in
                change worth {
                case .list1:
                    Textual content("Listing One")
                    .navigationTitle("Listing One")
                case .list2:
                    Textual content("Listing Two")
                    .navigationTitle("Listing Two")
                }
            }
            .navigationTitle("Lists")
        }
        
    }
}

ListsViewModel

ultimate class ListsViewModel: ObservableObject {
    @Printed var record: [ListsNavigation] = []
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments