ios - 在SwiftUI中点击Button时获取Tag值

标签 ios swift xcode button swiftui

SwiftUI 中,我创建了一个按钮循环。

点击按钮时,我必须获取标签值。

我已经尝试了很多次。我无法点击哪个按钮

HStack(spacing: 1) {
         ForEach(0...2, id: \.self) { j in
                            Button(action: {
                                // need button Tag
                               print("Tapped Button Tag:")
                            }, label: {
                                    Text("")
                            })
                            .tag("\(j)")
                        }
                    }

最佳答案

标签中不需要它,可以直接执行,例如

HStack(spacing: 1) {
         ForEach(0...2, id: \.self) { j in
                Button(action: {
                    // j is available here from context
                   print("Tapped Button Tag: \(j)")  // << here !!
                }, label: {
                        Text("")
                })
            }
        }

关于ios - 在SwiftUI中点击Button时获取Tag值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61853265/

相关文章:

ios - 为什么UICollectionViewCell的outlet在Interface Builder Xcode7.1 OS X Yosemite中为nil?

ios - 在indexpath.row之后快速追加到数组

ios - GameCenter iOS 排行榜标识符

swift - 通用方法覆盖不能快速工作

swift - fatal error : swapping a location with itself is not supported with Swift 2. 0

ios - UIView 不允许我添加 subview

ios - 如何将 32 位二进制文​​件添加到 UIKit for Mac 上的 libsodium?

javascript - 在保留范围链的同时将回调函数作为字符串传递

ios - UITextfield 子类没有响应 insertText : method of UIKeyInput protocol

ios - 仅选择行 1 次