SwiftUI - 文本字段 return 键事件
SwiftUI - Textfield return key event
首先,很抱歉我的英语不完美。
我想根据通过文本字段中的 return 键接收到的值的情况显示不同的图像。我搜索并尝试了几种方法,但找不到正确的方法。我在 SwiftUI 中找不到合适的 retrurn 键事件,所以我尝试了 onReceive。
swift 文件。
struct QuestionItemView: View {
@State private var question: Question?
@State private var answer = ""
var body: some View {
VStack(spacing: 30) {
GroupBox {
if let question = question {
Text(question.question)
.font(.largeTitle)
.fontWeight(.heavy)
.scaledToFit()
.frame(width: 300, height: 100)
.cornerRadius(12)
}
}
GroupBox {
TextField("Enter your answer", text: $answer)
.frame(width: 300)
.font(.title2)
.scaledToFit()
.cornerRadius(12)
.multilineTextAlignment(.center)
}
onReceive(answer.publisher) { _ in
if answer == question?.answer {
// Image(systemName: "circle")
print("o")
} else {
// Image(systemName: "multiply")
print("x")
}
}
} //: VStack
.frame(width: 240)
.background(RoundedRectangle(cornerRadius: 7.0).fill(Color.white))
.onAppear {
let questions: [Question] = Bundle.main.decode("questions.json")
question = questions.randomElement()
}
}
}
json.file
[
{
"id" : 1,
"question" : "2 X 6",
"answer" : "12"
},
{
"id" : 2,
"question" : "7 + 8",
"answer" : "15"
},
{
"id" : 3,
"question" : "9 - 1",
"answer" : "8"
},
{
"id" : 4,
"question" : "4 / 2",
"answer" : "2"
},
{
"id" : 5,
"question" : "10 X 2",
"answer" : "20"
}
]
你可以尝试这样的事情:
struct QuestionItemView: View {
@State private var question: Question?
@State private var answer = ""
@State private var imgType = ""
var body: some View {
VStack(spacing: 30) {
GroupBox {
if let question = question {
Text(question.question)
.font(.largeTitle)
.fontWeight(.heavy)
.scaledToFit()
.frame(width: 300, height: 100)
.cornerRadius(12)
}
}
GroupBox {
TextField("Enter your answer", text: $answer)
.frame(width: 300)
.font(.title2)
.scaledToFit()
.cornerRadius(12)
.multilineTextAlignment(.center)
.onSubmit { // <--- only on pressing the return key
// .onChange(of: answer) { _ in // <-- as you type
// .onReceive(answer.publisher) { _ in // <-- as you type
if answer == question?.answer {
imgType = "circle"
print("o")
} else {
imgType = "multiply"
print("x")
}
}
}
if imgType == "circle" {
Image(systemName: "circle")
} else {
Image(systemName: "multiply")
}
}
.frame(width: 240)
.background(RoundedRectangle(cornerRadius: 7.0).fill(Color.white))
.onAppear {
let questions: [Question] = Bundle.main.decode("questions.json")
question = questions.randomElement()
}
}
}
如果您使用的是旧系统,您可能需要这样做:
TextField("Enter your answer", text: $answer, onCommit: {
if answer == question?.answer {
imgType = "circle"
print("o")
} else {
imgType = "multiply"
print("x")
}
})
首先,很抱歉我的英语不完美。
我想根据通过文本字段中的 return 键接收到的值的情况显示不同的图像。我搜索并尝试了几种方法,但找不到正确的方法。我在 SwiftUI 中找不到合适的 retrurn 键事件,所以我尝试了 onReceive。
swift 文件。
struct QuestionItemView: View {
@State private var question: Question?
@State private var answer = ""
var body: some View {
VStack(spacing: 30) {
GroupBox {
if let question = question {
Text(question.question)
.font(.largeTitle)
.fontWeight(.heavy)
.scaledToFit()
.frame(width: 300, height: 100)
.cornerRadius(12)
}
}
GroupBox {
TextField("Enter your answer", text: $answer)
.frame(width: 300)
.font(.title2)
.scaledToFit()
.cornerRadius(12)
.multilineTextAlignment(.center)
}
onReceive(answer.publisher) { _ in
if answer == question?.answer {
// Image(systemName: "circle")
print("o")
} else {
// Image(systemName: "multiply")
print("x")
}
}
} //: VStack
.frame(width: 240)
.background(RoundedRectangle(cornerRadius: 7.0).fill(Color.white))
.onAppear {
let questions: [Question] = Bundle.main.decode("questions.json")
question = questions.randomElement()
}
}
}
json.file
[
{
"id" : 1,
"question" : "2 X 6",
"answer" : "12"
},
{
"id" : 2,
"question" : "7 + 8",
"answer" : "15"
},
{
"id" : 3,
"question" : "9 - 1",
"answer" : "8"
},
{
"id" : 4,
"question" : "4 / 2",
"answer" : "2"
},
{
"id" : 5,
"question" : "10 X 2",
"answer" : "20"
}
]
你可以尝试这样的事情:
struct QuestionItemView: View {
@State private var question: Question?
@State private var answer = ""
@State private var imgType = ""
var body: some View {
VStack(spacing: 30) {
GroupBox {
if let question = question {
Text(question.question)
.font(.largeTitle)
.fontWeight(.heavy)
.scaledToFit()
.frame(width: 300, height: 100)
.cornerRadius(12)
}
}
GroupBox {
TextField("Enter your answer", text: $answer)
.frame(width: 300)
.font(.title2)
.scaledToFit()
.cornerRadius(12)
.multilineTextAlignment(.center)
.onSubmit { // <--- only on pressing the return key
// .onChange(of: answer) { _ in // <-- as you type
// .onReceive(answer.publisher) { _ in // <-- as you type
if answer == question?.answer {
imgType = "circle"
print("o")
} else {
imgType = "multiply"
print("x")
}
}
}
if imgType == "circle" {
Image(systemName: "circle")
} else {
Image(systemName: "multiply")
}
}
.frame(width: 240)
.background(RoundedRectangle(cornerRadius: 7.0).fill(Color.white))
.onAppear {
let questions: [Question] = Bundle.main.decode("questions.json")
question = questions.randomElement()
}
}
}
如果您使用的是旧系统,您可能需要这样做:
TextField("Enter your answer", text: $answer, onCommit: {
if answer == question?.answer {
imgType = "circle"
print("o")
} else {
imgType = "multiply"
print("x")
}
})