【问题标题】:Is it possible to have a Button(action: -) in a List/ForEach view in SwiftUISwiftUI 的 List/ForEach 视图中是否可以有一个 Button(action: -)
【发布时间】:2020-05-04 07:06:46
【问题描述】:

我想将Button(action: - 视图添加到其中包含ForEach 的列表视图中,其中包含NavigationLink。因此,用户可以查看具有Button(action: - 的列表的详细信息屏幕,但是如果用户不需要查看详细信息,则可以在列表中使用Button 进行选择。

List {
  ForEach(DATA, id: \.self { data in
    NavigationLink(destination: DetailView(data: data)) {
      HStack {
        Text(data.name)
        Button(action: {
           // add to an array
        }) {
             Text(data.price)
        }
      }
    }
  }
}

但是当我点击按钮时,总是会进入 DetailView

不确定是否可行。任何建议

【问题讨论】:

    标签: swiftui swiftui-navigationlink


    【解决方案1】:

    这是可能的方法

    List {
      ForEach(DATA, id: \.self { data in
        NavigationLink(destination: DetailView(data: data)) {
          HStack {
            Text(data.name)
            Button(action: {
               // add to an array
            }) {
                 Text(data.price) // some padding might be useful
                    .foregroundColor(Color.blue) // << can be any 
            }.buttonStyle(PlainButtonStyle())    // << here !!
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      相关资源
      最近更新 更多