如何解决SwiftUI:检测@State变量的@Binding何时更改
我试图弄清楚如何从不同的View
更改值。这是我主要视图的实现:
import SwiftUI
import Combine
struct ContentView: View {
@State private var isPresented: Bool = false
@State private var textToProces = "" {
didSet{
print("text: oldValue=\(oldValue) newValue=\(textToProces)")
}
}
var body: some View {
ZStack{
VStack{
Button("Show Alert"){
self.isPresented = true
}.background(Color.blue)
}
ItemsAlertView(isShown: $isPresented,textToProcess: $textToProces)
}
}
}
在此视图下,我尝试更改textToProces
变量:
struct AnotherView: View {
@Binding var isShown: Bool
@Binding var textToProcess: String
var title: String = "Add Item"
let screenSize = UIScreen.main.bounds
var body: some View {
VStack {
Button(action: {
self.textToProcess = "New text"
self.isShown = false
},label: {
Text("dissmis")
})
Text(self.textToProcess)
}
.background(Color.red)
.offset(y: isShown ? 0 : screenSize.height)
}
}
当我更改此行self.textToProcess = "New text"
上的值时,主视图中的textToProcess
永远不会收到更改通知。我该怎么做才能在所有人都知道的主视图中获得更改通知?
非常感谢您的帮助
解决方法
您必须使用awful.screen.connect_for_each_screen
修饰符来跟踪对onChange
的更改。
textToProces
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。