英文:
Handling data flow in iOS 17 and using observed variables inside a TextField
问题
我正在尝试迁移到 iOS 17,并使用 @Observable
。
我有一个应用程序的全局视图模型,在其中我调用一个包含我需要的所有值的 Exam 模型。
根据 Apple 的指南,我使用我的视图模型如下:
import Foundation
import Observation
struct Exam: {
var title: String = ""
}
@Observable
final class Global {
var exam = Exam()
}
import SwiftUI
@main
struct TestApp: App {
@State private var model = Global()
var body: some Scene {
WindowGroup {
InfoView()
.environment(model)
}
}
}
接下来,我在 InfoView 中像这样使用我的模型:
struct InfoView: View {
// @Bindable private var model = Global()
@Environment(Global.self) private var model
var body: some View {
VStack {
TitledField(text: $model.exam.title, isMandatory: true, descriptionLabel: "", placeholder: "")
.submitLabel(.continue)
}
}
}
正如你所看到的,在第一个自定义文本字段中,我使用了 $model.exam.title
,但似乎不起作用,因为它给我报错:
无法在范围内找到 '$model'
所以我尝试了 @Bindable
,但用户在文本字段中插入的值未被读取,始终返回空字符串。我哪里出错了?有人已经开始使用 iOS 17 了吗?
英文:
I'm trying to migrate to iOS 17 using @Observable
.
I have a global view model of the app where I call an Exam model with all the values I need inside.
According to Apple guidelines I'm using my view model like this
import Foundation
import Observation
struct Exam: {
var title: String = ""
}
@Observable
final class Global {
var exam = Exam()
}
import SwiftUI
@main
struct TestApp: App {
@State private var model = Global()
var body: some Scene {
WindowGroup {
InfoView()
.environment(model)
}
}
}
Next I use my model within the InfoView like this
struct InfoView: View {
// @Bindable private var model = Global()
@Environment(Global.self) private var model
var body: some View {
VStack {
TitledField(text: $model.exam.title, isMandatory: true, descriptionLabel: "", placeholder: "")
.submitLabel(.continue)
}
}
}
As you can see in the first custom textField I use $model.exam.title
but it doesn't seem to work because it gives me this error
>Cannot find '$model' in scope
So I tried with @Bindable
but the value that the user inserts in the textField is not read and always returns an empty string where am I going wrong? Has anyone started working on iOS 17?
答案1
得分: 1
以下是翻译好的部分:
以下示例代码中使用@Bindable
在iOS17上对我有用。
当在TextField
中更改$model.exam.title
时,UI会刷新并显示更改后的值。
import SwiftUI
import Observation
@main
struct TestApp: App {
@State private var model = Global()
var body: some Scene {
WindowGroup {
InfoView(model: model) // <-- 这里
}
}
}
struct InfoView: View {
@Bindable var model: Global // <-- 这里
var body: some View {
VStack {
TextField("", text: $model.exam.title).border(.red)
.submitLabel(.continue)
Text(model.exam.title) // <-- 用于测试
}
}
}
struct Exam {
var title: String = "exam-title"
}
@Observable
final class Global {
var exam = Exam()
}
希望这对你有所帮助。如果有其他翻译需求,请随时告诉我。
英文:
The following example code with @Bindable
works for me on iOS17.
When the $model.exam.title
is changed in the TextField
,
the UI is refreshed and displays the changed value.
import SwiftUI
import Observation
@main
struct TestApp: App {
@State private var model = Global()
var body: some Scene {
WindowGroup {
InfoView(model: model) // <-- here
}
}
}
struct InfoView: View {
@Bindable var model: Global // <-- here
var body: some View {
VStack {
TextField("", text: $model.exam.title).border(.red)
.submitLabel(.continue)
Text(model.exam.title) // <-- for testing
}
}
}
struct Exam {
var title: String = "exam-title"
}
@Observable
final class Global {
var exam = Exam()
}
答案2
得分: 0
如果您正在使用@Environment
,您需要将其转换为@Bindable
,例如:
@Bindable var exam = model.exam
TitledField(text: $exam.title, isMandatory: true, descriptionLabel: "", placeholder: "")
@Bindable
不是DynamicProperty
,因此它可以像任何其他结构体一样在body
中使用。
英文:
If you are using @Environment
you need to convert to @Bindable
, e.g.
@Bindable var exam = model.exam
TitledField(text: $exam.title, isMandatory: true, descriptionLabel: "", placeholder: "")
@Bindable
isn't a DynamicProperty
so it can be used inside body
like any other struct.
答案3
得分: 0
一个很好的起点是:
我还添加了一个包含“add”的示例项目:
https://github.com/ingconti/iOS17_ObservableLibraryView
英文:
A good starting point is:
I added a sample project with "add" too:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论