import SwiftUI
class User: ObservableObject {
@Published var name = "Taylor Swift"
}
struct EditView: View {
ObservableObject var user: User
var body: some View{
TextField("Name", text:$user.name)
}
}
struct DisplayView:View {
@EnvironmentObject var user:User
var body: some View {
Text(user.name)
}
}
struct ContentView: View {
let user = User()
var body: some View {
VStack {
EditView()//.environmentObject(user)
DisplayView()//.environmentObject(user)
}.environmentObject(user)
}
}
SwiftUI:ObservableObject & @ObservableObject &environmentObject
最新推荐文章于 2024-03-16 00:02:27 发布