Search code examples
swiftlistbuttonswiftuinavigationlink

How to add a clickable button in a List without triggering NavigationLink event?


I have a button in a list item and when I click on it, the NavigationLink's event is also being triggered. I only want to trigger the button event. How to do it? following is my code:

ForEach(self.cares,id:\.id){ care in
    HStack{
          if(care.markMode == "打卡"){
               Button("今日未打卡"){
          }
          else{
               Button("新增记录"){
               self.care = care
               self.showSheetNewMark.toggle()
               }
          }
          NavigationLink(destination:CareDetailView(care: care)){
              Text("\(care.nickname!)的\(care.subject!)")
          }
          }
    }

Solution

  • Add PlainButtonStyle for button, as in below example

    Button("新增记录"){
      self.care = care
      self.showSheetNewMark.toggle()
    }
    .buttonStyle(PlainButtonStyle()) // << here !!