1

从 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")
                }
            }
        }
    }
}
4

0 回答 0