iOS Development

ios – SwiftUI NavigationLink: Anticipating New View Occasion on Every Navigation, However Created Solely As soon as

I’ve a SwiftUI app the place a fundamental display shows an inventory of things. The person can both navigate to an current merchandise by tapping on it or create a brand new merchandise by clicking a “+” icon. Every merchandise has a UUID, and it is essential that tapping the “+” icon at all times creates a brand new merchandise occasion, even when the person navigates again to the principle display with out saving the brand new merchandise.

This performance is vital on account of particular necessities in my app (particulars omitted for brevity).

I’ve seen that SwiftUI appears to solely create the element view occasion as soon as, even when navigating backwards and forwards. I count on a brand new occasion of the view (and the corresponding merchandise) every time the “+” is tapped, however this is not taking place. Widespread options I’ve discovered really feel extra like workarounds than correct implementations for what looks like a fundamental sample.

Is there a SwiftUI-centric strategy to making sure a brand new merchandise and its related view are created every time the “+” button is tapped?

import SwiftUI

struct ContentView: View {
    @StateObject var dataRepository = DataRepository()

    var physique: some View {
        NavigationView {
            VStack {
                Spacer()
                Listing {
                    ForEach(dataRepository.gadgets) { merchandise in
                        NavigationLink(vacation spot: DetailView(merchandise: merchandise, isAddScreen: false, dataRepository: dataRepository)) {
                            Textual content(merchandise.title)
                        }
                    }
                }
                .navigationBarItems(trailing: NavigationLink(vacation spot: DetailView(memo: nil, isAddScreen: true, dataRepository: dataRepository)) {
                    Picture(systemName: "plus")
                })
                .navigationBarTitle("Objects")
            }
        }
    }
}

struct DetailView: View {
    @Setting(.presentationMode) var presentationMode
    @State var merchandise: Merchandise?
    let isAddScreen: Bool
    let dataRepository: DataRepository

    init(merchandise: Merchandise?, isAddScreen: Bool, dataRepository: DataRepository) {
        print("it will print when the app launches") // <---------------------------- PROBLEM
        print("however not every subsequent time that we navigate right here!")
        self.isAddScreen = isAddScreen
        self.dataRepository = dataRepository
        if isAddScreen {
            let newItem = Merchandise(title: "New Merchandise") // <---------------------------- PROBLEM
            self._item = State(initialValue: newItem)
        } else {
            self._item = State(initialValue: merchandise!)
        }
    }

    var physique: some View {
        // Element view implementation...
    }
}

class DataRepository: ObservableObject {
    @Printed var gadgets: [Item] = [/* initial items */]
}

struct Merchandise: Identifiable {
    let id = UUID()
    var title: String
}

Credit: www.ismmailgsm.com

Leave a Reply

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

Back to top button