【问题标题】:Add button to list cell将按钮添加到列表单元格
【发布时间】:2019-07-02 05:08:09
【问题描述】:

如何向我的List 单元格添加按钮?

var body: some View{
    List {
        VStack {
            Button(action: {
                print("Hello, World!")
            }, label: {
                Text("Hello, World!")
            })
            Text("Something irrelevant")
        }
    }
}

当我点击Hello, World! 时,上面会打印Hello, World!,当我点击Something irrelevant 时也会打印。我只希望在点击按钮时发生这种情况,我该怎么做?

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    在 SwiftUI 中,当您点击列表中的单元格时,会调用每个子按钮的 action 参数。如果这不是您想要的行为,则需要配置按钮的tapAction 属性,同时将action 参数留空。

    在这种情况下,您的代码应该是这样的:

     var body: some View{
            List {
                VStack {
                    Button(action: {
    
                    }, label: {
                        Text("Hello, World!")
                    }).tapAction {
                        print("Hello, World!")
                    }
                    Text("Something irrelevant")
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多