【发布时间】:2021-12-01 07:52:48
【问题描述】:
我有以下代码:
struct BookView: View {
@State var title = ""
@State var author = ""
var body: some View {
TextField("Title", text: $title)
TextField("Author", text: $author)
}
}
struct MainView: View {
@State private var presentNewBook: Bool = false
var body: some View {
NavigationView {
// ... some button that toggles presentNewBook
}.sheet(isPresented: $presentNewBook) {
let view = BookView()
view.toolbar {
ToolbarItem(placement: principal) {
TextField("Title", text: view.$title)
}
}
}
}
}
这会编译,但在运行时给我以下错误:
Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not update.
如何将状态变量传递给其他外部视图?我不能在BookView 上使用ObservableObject,因为这需要我将它从struct 更改为class
【问题讨论】: