Update environment object on listRow click before navigation

Here I have created a observable object, which I added in environment object. On list row click I need to update some values of my environment object before navigating on DetailView. Simply I want to show detail of object based on row selection.

Here is the code I tried:

class MyModel:ObservableObject 
{
    var selectDate: String
    var duration: String
    var selectProject: String

    init(dateStr: String, durationStr: String, projectName: String) {
        self.selectDate = dateStr
        self.duration = durationStr
        self.selectProject = projectName
    }

}

struct ContentView: View {
    @EnvironmentObject var model: MyModel
    @Environment (.colorScheme) var colorScheme:ColorScheme
    @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
    @ObservedObject var viewModel = TimesheetViewModel()
    var body: some View {
      List {                                      
          ForEach(self.viewModel.tasksArr, id:.id) { taskObj in
               NavigationLink(destination: DetailView()) { >need to update **model** before navigating Detail View
                 TimeSheetRowView(taskObj:rowElement)
           }
   }
}


Is there any other way to perform same task? Help me out in this

If you have to support iOS 13-14 you can use .onAppear attached to the destination.

If you have to support iOS 15 you can use .task attached to the destination.

struct Store{
    var items: [Task] = [.init(name: UUID().uuidString), .init(name: UUID().uuidString), .init(name: UUID().uuidString)]
    var selected: Task?
    struct Task: Identifiable{
        let id: UUID = .init()
        let name: String
    }
}

@available(iOS 15.0, *)
struct OnSelectView: View {
    @Environment (.colorScheme) var colorScheme:ColorScheme
    @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
    @State var store: Store = .init()
    var body: some View {
        NavigationView{
            List {
                ForEach(store.items, id:.id) { taskObj in
                    NavigationLink(destination:
                        detailView(taskObj)
                        //.onAppear{ //or
                        .task {
                            //do something here
                            store.selected = taskObj
                        }
                    ) {
                        Text(taskObj.name)
                    }
                }
            }
        }
    }
    
    @ViewBuilder func detailView (_ task: Store.Task) -> some View{
        VStack{
            Text(task.name)
            if let selected = store.selected{
                Text(selected.name)
            }else{
                ProgressView()
            }
        }
    }
}

The options above are not technically perform before navigation they are performed as the view “appears”.

If you have to support iOS 16 you should use NavigationStack along with NavigationPath and navigationDestination.

@available(iOS 16.0, *)
struct OnSelectView: View {
    @Environment (.colorScheme) var colorScheme:ColorScheme
    @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
    @State var store: Store = .init()
    @State var path: NavigationPath = .init()
    var body: some View {
        NavigationStack(path: $path){
            List {
                ForEach(store.items, id:.id) { taskObj in
                    Button {
                        //Do something
                        store.selected = taskObj
                        
                        path.append(taskObj)
                    } label: {
                        HStack{
                            Text(taskObj.name)
                            Spacer()
                            Image(systemName: "chevron.forward")
                                .foregroundColor(Color(UIColor.placeholderText))
                        }
                    }.buttonStyle(.plain)
                }
            }.navigationDestination(for: Store.Task.self) { task in
                detailView(task)
            }
        }
    }
    
    @ViewBuilder func detailView (_ task: Store.Task) -> some View{
        VStack{
            Text(task.name)
            if let selected = store.selected{
                Text(selected.name)
            }else{
                ProgressView()
            }
        }
    }
}

2

You can use a Button and an empty NavigationLink. When your button is tapped you programmatically select a NavigationLink but before you can perform some actions:

@State private var selectedItem: String?

var body: some View {
    NavigationView {
        List {
            ForEach(self.viewModel.tasksArr, id:.id) { taskObj in
                HStack {
                    Button(action: {
                        // here you can update your model etc.
                        self.selectedItem = item
                    }, label: {
                        TimeSheetRowView(taskObj:rowElement)
                    })
                    NavigationLink(destination: DetailView(), tag: item, selection: self.$selectedItem) {
                        EmptyView()
                    }
                }
            }
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật