SwiftUI变量state设置步骤

2024-12-06

state标注

改变后需要重新渲染的变量使用@state标注,如果是object则用@StateObject。

@State var showingSettings = false
@StateObject var viewModel = ContentViewModel()

class添加ObservableObject的protocol

class ContentViewModel: ObservableObject {
}

需要暴露的变量,使用@Published

class ContentViewModel: ObservableObject {
	@Published var title = ""
}