TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

LazyVStack in NavigationView goes to parent view after changing anything in environmentObject

Forums > SwiftUI

Parent view with lazyVSTack in navigation View

 NavigationView {
            Form{
            ScrollView{
                LazyVStack(alignment: .leading, spacing: 0){

                    ///Showing all the available list after filtering
                    ForEach(filteredList){ checkList in
                        NavigationLink(
                            destination: ItemList(checkList: checkList).environmentObject(modelData),
                            tag: checkList.id,
                            selection: $modelData.listSelector
                        ){
                            ListRow(checkList: checkList)
                                .addButtonActions(leadingButtons: pinUnpinButton(checkList: checkList),
                                                  trailingButtons: [.info, .delete],
                                                  onClick: { button in
                                                    sendSwipeButtonClick(Button: button, checkList: checkList)
                                                  })
                                .sheet(isPresented: $editSheet) {
                                    ListInfo(showInfo: $editSheet, draftList: $draftList)
                                        .environmentObject(modelData)
                                }
                        }
                    }
                }
                }
 }

Child view. when i change anything in the modelData and if there are more than one list in the parent view it returns to parent view and not stay in child view. And when the navigation link selector is modified to remain in the child view it goes to parent view then comes back to child view and then goes back again to parent view even after using DispatchQuene.main.asyncAfter(deadline: .now() + 0.20)

List(){
            ForEach(filteredItems) { item in

                HStack{

///                 Completed button
                    ToggleCompleted(item: item, indexList: indexList)
                        .environmentObject(modelData)

///                 Item name
                    ItemName(item: item)

                    Spacer()

///                 quantity
                    if modelData.checkLists[indexList].showQuantity {

                        Text("\(item.itemQuantity)")
                            .foregroundColor(.gray)
                            .font(.subheadline)
                            .padding()
                    }

///                 info button
                    Button{
                        showInfo.toggle()
                        draftItem = item
                    } label: {
                        Image(systemName: "info.circle")
                            .resizable()
                            .frame(width: 25, height: 25, alignment: .center)
                            .foregroundColor(modelData.checkLists[indexList].color)

                    }
                    .sheet(isPresented: $showInfo) {
                        ItemInfo(showInfo: $showInfo, draftItem: $draftItem, checkList: checkList)
                            .environmentObject(modelData)

                    }

                }
            }

            ///Drag to delete
            .onDelete(perform: { indexSet in
                ///removing notification
                for i in indexSet {
                    AppNotification().remove(ID: modelData.checkLists[indexList].items[i].id)
                }

                modelData.checkLists[indexList].items.remove(atOffsets: indexSet)
            })
}

I am fairly new to SwiftUI and ios app development.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.