我的HomeView
(我存储Movies
的列表)有NavigationView
和NavigationLink
,目的地为DetailView
。
当我想在NavigationBarItems
中添加DetailView
时,它会使我的 GoBack幻灯片(从DetailView
到HomeView
)无效。当我停止在屏幕的约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)
}
}
}
}
答案 0 :(得分:2)
这是SwiftUI的错误。如果您的.sheet
中有.alert
和/或DetailView
,请将其删除,它会按预期运行,而不会冻结应用程序。