从 pages 数组的末尾删除项目会导致在浏览页面时出错。有没有办法根据需要从 TabView
动态删除视图?
import SwiftUI
struct ContentView: View {
@State var pages = ["1", "2", "3", "4"]
var body: some View {
NavigationView {
TabView {
ForEach(pages, id: \.self) { page in
Text(page)
}
}
.tabViewStyle(PageTabViewStyle())
.toolbar {
Button(action: {
pages.removeLast()
}) {
Image(systemName: "trash")
}
}
}
}
}