SwiftUI NavigationBarItems slideBack冻结应用程序

时间:2019-12-15 14:16:44

标签: ios swiftui navigationbar back-button

我的HomeView(我存储Movies的列表)有NavigationViewNavigationLink,目的地为DetailView

当我想在NavigationBarItems中添加DetailView时,它会使我的 GoBack幻灯片(从DetailViewHomeView)无效。当我停止在屏幕的约1/3处滑动时,该应用程序冻结。

我在NavigationView中没有其他DetailView,因为当我拥有它时,我在DetailView中将其增加了一倍。

我发现破坏所有内容的代码行。

NavigationBarItems的一部分:

.navigationBarItems(trailing: Button(action: {
    self.showingEditScreen.toggle()
}) {
    Image(systemName: "pencil")
    .imageScale(.large)
    .accessibility(label: Text("Edit Movie"))
    .padding()
})

还有HomeView

struct HomeView: View {
    @Environment(\.managedObjectContext) var moc

    @FetchRequest(entity: Movie.entity(), sortDescriptors: [
        NSSortDescriptor(keyPath: \Movie.title, ascending: true),
        NSSortDescriptor(keyPath: \Movie.director, ascending: true)
    ]) var movies: FetchedResults<Movie>

    @State private var showingAddScreen = false

    func deleteMovie(at offsets: IndexSet) {
        for offset in offsets {

            let movie = movies[offset]

            moc.delete(movie)
        }
        try? moc.save()
    }


    var body: some View {
        NavigationView {
            List {
                ForEach(movies, id: \.self) { movie in
                    NavigationLink(destination: DetailMovieView(movie: movie)) {
                        EmojiRatingView(rating: movie.rating)
                            .font(.largeTitle)
                        VStack(alignment: .leading) {
                            Text(movie.title ?? "Unknown Title")
                                .font(.headline)
                            Text(movie.director ?? "Unknown Director")
                                .foregroundColor(.secondary)
                        }
                    }
                }
                .onDelete(perform: deleteMovie)
            }
            .navigationBarTitle("Movie Store")
            .navigationBarItems(leading: EditButton(), trailing: Button(action: {
                self.showingAddScreen.toggle()
            }) {
                Image(systemName: "plus")
                    .imageScale(.large)
                    //.accessibility(label: Text("Add Movie"))
                    .padding()
            })
                .sheet(isPresented: $showingAddScreen) {
                    AddMovieView().environment(\.managedObjectContext,     self.moc)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这是SwiftUI的错误。如果您的.sheet中有.alert和/或DetailView,请将其删除,它会按预期运行,而不会冻结应用程序。