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