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

心不动则不痛 提交于 2021-02-11 13:46:41

问题


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!)")
          }
          }
    }

回答1:


Add PlainButtonStyle for button, as in below example

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


来源:https://stackoverflow.com/questions/61848190/how-to-add-a-clickable-button-in-a-list-without-triggering-navigationlink-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!