![](/img/trans.png)
[英]Add conditions on shorthand argument name $0 that returns one or two different text views in SwiftUI
[英]Add Button by id for SwiftUI inside ForEach using shorthand argument name $0
如何在ForEach
添加Button
? 適用於Text
但不適用於Button
。 想使用速記參數名稱$0
import SwiftUI
struct ContentView: View {
let emojiMoves = ["🗿", "🧻", "✂️"]
var body: some View {
HStack {
ForEach(emojiMoves, id: \.self) {
Text($0)
}
}
}
}
像這樣的東西可以工作,但我喜歡$0
的簡單性
ForEach(0 ..< self.emojiMoves.count, id: \.self ) { number in
Button(action: {
self.emojiMoves[(number)]
})
}
你可以試試
ForEach(self.emojiMoves, id: \.self ) {
Button($0){
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.