-2
import SwiftUI

struct ListView: View {
    
    @EnvironmentObject var listViewModel: ListViewModel
    
    @State var isTitleEdited: Bool = false
    
    var body: some View {
        ZStack {
            if listViewModel.items.isEmpty {
                NoItemsView()
            } else {
                List {
                    ForEach(listViewModel.items) { item in
                        ListRowView(item: item)
                            .onTapGesture (count:2){
                                withAnimation(.linear) {
                                    listViewModel.updateItem(item: item)
                                }
                            }
                            .onTapGesture {
                                NavigationLink(
                                    destination: AddView(),
                                    label: {
                                        Text (item.title)
                                    }
                                )
                            }
                    }
                    .onDelete(perform: listViewModel.deleteItem)
                    .onMove(perform: listViewModel.moveItem)

                }
                .listStyle(InsetListStyle())
            }
        }
        .navigationTitle("Todo List and Memo")
        .navigationBarTitleDisplayMode(.automatic)
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                EditButton()
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                NavigationLink(destination: AddView(),
                               label: {
                    Image(systemName: "plus")
                        .foregroundColor(.red)
                })
            }
        }
    }
}

struct ListView_Previews: PreviewProvider {
    static var previews: some View {
        
        NavigationView {
            ListView()
        }
        .environmentObject(ListViewModel())
    }
}

I MAKE A TODOLIST

I WANT TO MAKE CHANGE WHEN I TAP THE TODOLIST

WHEN TAP THE TODOLIST 2 TIMES MAKES TODOLIST COMPLETED

WHEN TAP THE TODOLIST 1 TIME MAKES TITLE OF TODOLIST EDITED

SO I WANT TO REUSE THE ADDVIEW(), WHEN TAP THE TODOLIST 1 TIME, WITH NAVIGATIONLINK

BUT IT DOESN`T WORK....

Jonas Lang
  • 213
  • 1
  • 2
  • 12
권정근
  • 1
  • 1

1 Answers1

0

NavigationLink is a view. Don't use it in an closure. My suggestion should work, but could not test it because there are to many specific things in your example. Try to use code that works out of the box, so everybody can test.

List {
    ForEach(listViewModel.items) { item in
        NavigationLink(
            destination: AddView(),
            label: {
                ListRowView(item: item)
                    .onTapGesture (count:2){
                        withAnimation(.linear) {
                            listViewModel.updateItem(item: item)
                        }
                    }
            }
        )
    }
    .onDelete(perform: listViewModel.deleteItem)
    .onMove(perform: listViewModel.moveItem)
    
}
.listStyle(InsetListStyle())
Jonas Lang
  • 213
  • 1
  • 2
  • 12
  • Thnank you !! one more thing... after you comment, it`s works that when i click the item in ListRowView, View changed to AddView() but when i click two times it didn`t update Item... – 권정근 Mar 12 '23 at 00:12
  • For that you have to implement your own NavigationLink, because Apple's directly navigates after the first click. Maybe a better way is use `.swipeActions(edge: HorizontalEdge, allowsFullSwipe: Bool, content: () -> View)`, so that the user knows what he can do. – Jonas Lang Mar 12 '23 at 16:49