1

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

2 Answers 2

1
+50

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()
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The iOS 16 solution seems SO CLOSE! The area that the Spacer() occupies does not seem tappable. Only the elements on either side of it trigger the button. Ideas?
Ah, it looks like modifying the HStack with .contentShape(Rectangle()) fixes that. I think this is sufficient!
0

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()
                    }
                }
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.