[英]value being constant in swiftUI
I am sorry im not sure that the title makes sense but if u read, im sure u will understand my problem.很抱歉,我不确定标题是否有意义,但如果您阅读,我相信您会理解我的问题。 I have declared a variable @State var timetext: Int32
in the file CreatingWorkout, with a textfield TextField("5000, 100 etc", value: $timetext, formatter: NumberFormatter())
When i go to the createWorkoutView file and try to present it with the sheet, it wants me to give a value to timetext.我在文件 CreatingWorkout 中声明了一个变量@State var timetext: Int32
,带有文本字段TextField("5000, 100 etc", value: $timetext, formatter: NumberFormatter())
当我将 go 发送到 createWorkoutView 文件并尝试呈现它与工作表一起,它希望我给 timetext 一个值。 However, when i provide a value with the textfield it stays constantly value given when calling with the sheet.但是,当我为文本字段提供一个值时,它在使用工作表调用时始终保持给出的值。 I will attach a video here for you to see.我会在这里附上一个视频给你看。
CreatingWorkout.swift:创建锻炼.swift:
struct CreatingWorkout: View {
@State var workoutTitle: String
@State var desc: String
@State var timetext: Int32
@State private var iconColor = Color.black
@State var displayWorkout: String = ""
@Environment(\.dismiss) var dismiss
@Environment(\.managedObjectContext) private var viewContext
private func saveWorkout() {
do {
let workout = Workout(context: viewContext)
workout.title = workoutTitle
workout.time = timetext
workout.icon = displayWorkout
workout.descriptionn = desc
try viewContext.save()
} catch {
print(error.localizedDescription)
}
}
CreateWorkout.swift:创建锻炼.swift:
import SwiftUI
struct CreateWorkoutView: View {
@State private var showingCreateWorkout = false
@State var timetext: Int32 = 0
var body: some View {
NavigationView {
VStack {
VStack(alignment: .center, spacing: 20) {
Text("Fitzy")
.font(.largeTitle)
.fontWeight(.bold)
Text("Create your first Workout")
.font(.title3)
.foregroundColor(.gray)
Button {
showingCreateWorkout.toggle()
} label: {
Image(systemName: "plus")
.font(.largeTitle)
.foregroundColor(.white)
.padding()
.background(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.foregroundColor(Color("AccentColor"))
.frame(width: 50, height: 50)
)
}.sheet(isPresented: $showingCreateWorkout) {
CreatingWorkout(workoutTitle: "", desc: "", timetext: timetext)
}
}
}.navigationTitle("Create Workout")
}
}
}
struct CreateWorkoutView_Previews: PreviewProvider {
static var previews: some View {
CreateWorkoutView()
}
}
https://drive.google.com/file/d/1qQDQmap5bMz9LxzibV98epHW5urqUtZ7/view?usp=sharing
as mentioned in the comments, you need a @State
property to pass the value that you type in your TextField
to the sheet with CreatingWorkout
.如评论中所述,您需要一个@State
属性将您在TextField
中键入的值传递给带有CreatingWorkout
的工作表。 Try something like this:尝试这样的事情:
struct CreatingWorkout: View {
@State var workoutTitle: String
@State var desc: String
@State var timetext: Int32
// ....
var body: some View {
Text("\(timetext)")
}
}
struct CreateWorkout: View {
@State var showingCreateWorkout = false
@State var timetext: Int32 = 0 // <-- here
var body: some View {
VStack {
TextField("type a number", value: $timetext, format: .number).border(.red) // <-- here
Button {
showingCreateWorkout.toggle()
} label: {
Image(systemName: "plus").font(.largeTitle).foregroundColor(.red).padding()
.background(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.foregroundColor(Color("AccentColor"))
.frame(width: 50, height: 50)
)
}.sheet(isPresented: $showingCreateWorkout) {
CreatingWorkout(workoutTitle: "", desc: "", timetext: timetext) // <-- here
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.