Wednesday, June 7, 2023
HomeiOS Developmentios - SwiftUI NavigationStack: Surprising Navigation Conduct from Record View

ios – SwiftUI NavigationStack: Surprising Navigation Conduct from Record View


I am constructing an iOS app with SwiftUI and am encountering sudden behaviour when navigating from a Record view to a element view.

I’ve three views: ViewA, ViewB, and ViewC. ViewA navigates to ViewB, which is an inventory of Merchandise cases. Tapping on an Merchandise in ViewB ought to navigate to ViewC, presenting the small print of the chosen Merchandise.

The navigation to ViewC is misbehaving. After I faucet an Merchandise in ViewB, ViewC is proven briefly after which the view instantly navigates again to ViewB. The navigation stack has ViewC earlier than ViewB. Stack: [ViewA, ViewC, ViewB]

The minimal reproducible instance:

struct Merchandise: Identifiable, Hashable {
    let id = UUID()
    let title: String
    let description: String
}

struct ViewA: View {
    var physique: some View {
        NavigationStack {
            NavigationLink("Go to ViewB") {
                ViewB()
            }
        }
    }
}

struct ViewB: View {
    let objects: [Item] = [
        Item(title: "Item 1", description: "This is item 1"),
        Item(title: "Item 2", description: "This is item 2")
    ]
    
    var physique: some View {
        Record(objects, id: .id) { merchandise in
            NavigationLink(worth: merchandise) {
                Textual content(merchandise.title)
            }
        }
        .navigationDestination(for: Merchandise.self) { merchandise in
            ViewC(merchandise: merchandise)
        }
    }
}

struct ViewC: View {
    let merchandise: Merchandise

    var physique: some View {
        Textual content(merchandise.description)
    }
}

I count on to navigate on to ViewC when an Merchandise is chosen in ViewB and the again button ought to take me again to ViewB. The stack needs to be [ViewA (root), ViewB, ViewC].

What induced the misbehaviour and the way can I resolve this?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments