variables - 如何在 Swiftui 中更改不同文件中的变量值
问题描述
我在内容视图中设置了一个变量@State var shouldShowModal = false
,我想在按下按钮后更改它shouldShowModal = false
。我一直在范围内找不到“shouldShowModal”。
解决方案
这是一个工作示例,通过@Binding
s 传递值。阅读更多关于@Binding
这里,或官方文档。
这意味着您现在可以shouldShowModal = false
使用绑定,这也将更新包含@State
.
struct ContentView: View {
@State private var shouldShowModal = false
var body: some View {
VStack {
Text("Hello world!")
.sheet(isPresented: $shouldShowModal) {
Text("Modal")
}
OtherView(shouldShowModal: $shouldShowModal)
}
}
}
struct OtherView: View {
@Binding var shouldShowModal: Bool
var body: some View {
VStack {
Text("Should show modal: \(shouldShowModal ? "yes" : "no")")
Toggle("Toggle modal", isOn: $shouldShowModal)
}
}
}
推荐阅读
- kubernetes - 挂载路径错误:不是目录:未知
- javascript - 如何使用 Vue 中的 post 方法将对象从一个端点发送到另一个端点?
- sql-server - 使用 Telegraf 和 Influx 监控 Azure SQL 数据库
- angular - 使用 ngx 数据表切换列
- java - 我怎样才能打破一个字符串
标记到地图集合中并为其分配布尔值(如果存在)? - php - Outlook 中的 IMAP_OPEN 无法使用 PHP
- python - 读取 csv 文件时 MS Databricks Spark 中绝对 URI 中的相对路径
- sapui5 - sap.tnt.NavigationListItem 的稳定 ID(id 与键)
- javascript - 对购物车部分中的产品列表做出反应
- postgresql - 在 postgreSQL 中将文本转换为时间戳