赞
踩
struct ContentView: View {
@State private var tapCount = 0
var body: some View {
Button("Tap count: \(tapCount)") {
tapCount += 1
}
}
}
struct User {
var firstName = "Bilbo"
var lastName = "Baggins"
}
struct ContentView: View {
@State private var user = User()
var body: some View {
VStack {
Text("Your name is \(user.firstName) \(user.lastName).")
TextField("First name", text: $user.firstName)
TextField("Last name", text: $user.lastName)
}
}
}
struct User {}
// -->
class User {}
class User: ObservableObject {
var username = "@twostraws"
}
struct ContentView: View {
@StateObject var user = User()
var body: some View {
Text("Username: \(user.username)")
}
}
// An example class to work with class Player: ObservableObject { @Published var name = "Taylor" @Published var age = 26 } // A view that creates and owns the Player object. struct ContentView: View { @StateObject var player = Player() var body: some View { NavigationView { NavigationLink(destination: PlayerNameView(player: player)) { Text("Show detail view") } } } } // A view that monitors the Player object for changes, but // doesn't own it. struct PlayerNameView: View { @ObservedObject var player: Player var body: some View { Text("Hello, \(player.name)!") } }
class Order: ObservableObject {
@Published var items = [String]()
}
struct ContentView: View {
@ObservedObject var order: Order
var body: some View {
// your code here
}
}
class UserProgress: ObservableObject {
@Published var score = 0
}
class UserProgress: ObservableObject { @Published var score = 0 } struct InnerView: View { @ObservedObject var progress: UserProgress var body: some View { Button("Increase Score") { progress.score += 1 } } } struct ContentView: View { @StateObject var progress = UserProgress() var body: some View { VStack { Text("Your score is \(progress.score)") InnerView(progress: progress) } } }
class Order: ObservableObject {
@Published var items = [String]()
}
struct ContentView: View {
@EnvironmentObject var order: Order
var body: some View {
// your code here
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。