我想知道在SwiftUI(我撰写本文时为Xcode 11 beta 6)中onAppear和Disappear的行为是开发人员会发现更有用的东西,还是仅仅是一个问题而不是功能。
现在,如果我们使用级联导航,就像您在我附加的示例代码中发现的那样(在Xcode 11b6中编译并运行良好),则来回导航的用户的控制台输出只会在出现以下情况时触发onAppear:向前的新视图加载(意味着更深入)。
在导航中:根-> NestedView1-> NestedView2-> NestedView3,在向每个视图阶段添加调试助手时,
.onAppear(perform: {print("onAppear level N")})
.onDisappear(perform: {print("onDisappear level N")})
Run Code Online (Sandbox Code Playgroud)
调试控制台将显示
onAppear root level 0
onAppear level 1
onAppear level 2
onAppear level 3
(没有onDisappear触发)
但是返回根<-NestedView1 <-NestedView2 <-NestedView3
调试控制台将不显示任何内容
(没有onAppear或onDisappear触发)
struct NestedViewLevel3: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
VStack {
Spacer()
Text("Level 3")
Spacer()
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Back")
.padding(.horizontal, 15)
.padding(.vertical, 2)
.foregroundColor(Color.white)
.clipped(antialiased: true)
.background(
RoundedRectangle(cornerRadius: 20)
.foregroundColor(Color.blue)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, …Run Code Online (Sandbox Code Playgroud) 给定一个带有 EditButton() 的列表视图,如果正在编辑该视图(用户点击“编辑”按钮),当用户离开该视图时,如何以编程方式禁用编辑模式?(例如,与 iPhone 上的“电话”应用程序中的“收藏夹”视图类似的行为)
struct ContentView2: View {
@State var fruits = ["Apple", "Banana"]
var body: some View {
NavigationView {
List {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
.onDelete { _ in
print("Delete")
}
}
.toolbar {
EditButton()
}
}
.onDisappear {
// make List's editMode be .inactive; how?
}
}
}
Run Code Online (Sandbox Code Playgroud)