我有一个简单的应用,其中包含4个标签,每个标签中都有一个文本字段。在应用程序打开时,将按预期方式打印“ 1出现”。
但是,在选项卡1中,当将TextField聚焦为“ 2出现”,“ 3出现”和“ 4出现”时,为什么那些onAppear闭包正在运行?
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
var body: some View {
TabView {
SimpleView()
.tabItem {
Text("1")
}.onAppear{
print("1 appear")
}
SimpleView()
.tabItem {
Text("2")
}.onAppear{
print("2 appear")
}
SimpleView()
.tabItem {
Text("3")
}.onAppear{
print("3 appear")
}
SimpleView()
.tabItem {
Text("4")
}.onAppear{
print("4 appear")
}
}
}
}
import SwiftUI
struct SimpleView: View {
@State var text: String = ""
var body: some View {
TextField("Enter text", text: self.$text)
}
}