FirstView Appeared
打印两次。一次是在视图首次加载时,一次是在选择 NavigationLink 时。
import SwiftUI
struct FirstView: View {
var body: some View {
NavigationView{
ZStack{
Text("FirstView").onAppear(perform: {print("FirstView Appeared")})
NavigationLink(destination: SecondView()) {
Text("Goto SecondView")
}.offset(y: 50)
}
}
}
}
struct SecondView: View {
var body: some View {
Text("SecondView").onAppear(perform: {print("SecondView Appeared")})
}
}
在选择导航链接时,在模拟器和个人设备上同时在XCode 12.0 Beta中运行代码:
FirstView Appeared
FirstView Appeared
SecondView Appeared
这是 onAppear() 预期行为的重复吗?
如果是这样,有什么最佳实践可以在firstview
创建时加载一些数据,然后在返回时firstview
加载(因为 onAppear() 会在导航离开时尝试加载一些数据firstView
)