为 SwiftUI 创建一个基于文本的游戏
想知道为什么我不能在字符 ForEach 循环中访问 isSelected?
收到此错误:无法在此行将“Bool”类型的值转换为预期的参数类型“Binding”
Toggle(isOn: character.isSelected){
变量对象声明:
@Binding var characters: [Character]
代码在这里:
VStack {
ForEach(characters) { character in
HStack{
VStack(alignment:.leading) {
Text("\(character.name)")
.fontWeight(.bold)
Text("\(character.description)")
.lineLimit(10)
}
Spacer()
Toggle(isOn: character.isSelected){
Text("a")
}.labelsHidden()
Run Code Online (Sandbox Code Playgroud) 我以前尝试过将.sheet附加在列表上,并且可以正常工作。但是出于我的项目目的,我想使用一个函数来调用模式警报。
import SwiftUI
struct Battlefield : View {
@State var isModalInputPresented: Bool = false
@State var inputTitle: String = ""
@State var inputMessage: String = ""
@State var inputValue: Int = 0
private func summonModalInput(title: String, message: String, input: Int) {
self.isModalInputPresented.toggle()
self.inputTitle = title
self.inputMessage = message
self.inputValue = input
sheet(isPresented: $isModalInputPresented, content: { InputView(inputTitle: self.$inputTitle, inputMessage: self.$inputMessage, inputValue: self.$inputValue, isPresented: self.$isModalInputPresented)})
}
var body: some View {
summonModalInput(title: "foo", message: "bar", input: 0)
}
}
Run Code Online (Sandbox Code Playgroud)