Saturday, January 6, 2024
HomeiOS Developmentios - Protect Navigation State in SwiftUI

ios – Protect Navigation State in SwiftUI


I’ve a Drawback preserving the Navigation state in my iOS App.
I’ve a NavigationSplitView (sidebar) with an inventory of NavigationLinks.
Every NavigationLink Navigates to a View With a NavigationStack and its personal NavigationLinks with NavigationDestinations and a Navigation Path. I need to save the trail utilizing @ScenenStorage (after serialization) so after I navigate utilizing the Sidebar, the NavigationStates of the Views are preserved. sadly I get the next message:

Word: Hyperlinks seek for locations in any surrounding NavigationStack, then throughout the identical column of a NavigationSplitView.
A NavigationLink is presenting a worth of kind “String” however there isn’t a matching navigationDestination declaration seen from the situation of the hyperlink. The hyperlink can’t be activated.

Word: Hyperlinks seek for locations in any surrounding NavigationStack, then throughout the identical column of a NavigationSplitView.

The Navigation solely works as soon as. And solely earlier than navigation through the Sidebar.

The code appears like this:

MainView:

    var physique: some View {
        NavigationSplitView(
            sidebar: { SidebarView() },
            element: { HomeView()}
        )
    }
}

NavigationLinks within the Sidebar:

struct SidebarNavigationItem<Vacation spot: View>: View {
    
    var possibility: String
    var icon: String
    var vacation spot: Vacation spot
    let arsColors = ARSColors.shared
    
    init(possibility: String, icon: String, vacation spot: Vacation spot) {
        self.possibility = possibility
        self.icon = icon
        self.vacation spot = vacation spot
    }
    
    var physique: some View {
        NavigationLink(vacation spot: vacation spot) {
            Label {
                Textual content(possibility)
                    .foregroundColor(arsColors.main)
            } icon: {
                Picture(systemName: icon)
                    .foregroundColor(arsColors.spotlight)
            }
        }
    }
}

HomeView (that is the Navigation I need to protect)

import SwiftUI

extension [String]: RawRepresentable {
    public init?(rawValue: String) {
        guard let information = rawValue.information(utilizing: .utf8),
              let outcome = attempt? JSONDecoder().decode([String].self, from: information)
        else {
            return nil
        }
        self = outcome
    }
    
    public var rawValue: String {
        guard let information = attempt? JSONEncoder().encode(self),
            let outcome = String(information: information, encoding: .utf8)
        else {
            return "[]"
        }
        return outcome
    }
}

struct HomeView: View {
    
    @SceneStorage("navPath") var navPath: [String] = []
    
    var physique: some View {
        NavigationStack(path: $navPath) {
            VStack {
                Spacer()
                
                HStack {
                    NavigationLink(worth: navPath) {
                        WidgetView(widgetText: "Testnavigation")
                    }
                    
                    Spacer()
                    WidgetView(widgetText: "Wifget2")
                    Spacer()
                    WidgetView(widgetText: "Widget3")
                    Spacer()
                    WidgetView(widgetText: "Widget4")
                    Spacer()
                    WidgetView(widgetText: "Widget5")
                }
                .padding(.all)
            }
            .navigationTitle("Dwelling")
            .navigationDestination(for: [String].self) {navPath in
                Test4NavView(navPath: navPath)
            }
        }
        .onAppear(){
            print(navPath)
        }
        .onDisappear() {
            print(navPath)
        }
    }
}

And at last the TestView


struct Test4NavView: View {
    
    var navPath: [String]
    
    @SceneStorage("input1") non-public var input1: String = ""
    @SceneStorage("input2") non-public var input2: String = ""
    @SceneStorage("input3") non-public var input3: String = ""
    @SceneStorage("input4") non-public var input4: String = ""

    
    var physique: some View {
        VStack(alignment: .heart) {
            
            TextField("Enter 1", textual content: $input1)
            TextField("Enter 2", textual content: $input2)
            TextField("Enter 3", textual content: $input3)
            TextField("Enter 4", textual content: $input4)
        }
        .onAppear(){
            print(navPath)
        }
        .onDisappear() {
            print(navPath)
        }
    }
}

My Expectation is to Navigate to HomeView utilizing the Sidebar then to Test4NavView from HomeView then to a distinct View utilizing the Sidebar then again to HomeView utilizing the Sidebar and I find yourself on Test4NavView



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments