SwiftUI PresentationMode.wrappedValue.dismiss()弹出回到根目录

时间:2020-10-27 12:22:00

标签: swiftui swiftui-navigationlink

在我的项目中,我试图从表单中保存数据后,使用SwiftUI中的presentationMode.wrappedValue.dismiss()弹出回到先前的视图控制器。

布局如下:

根控制器(带有列表视图)->列表视图->添加新数据 使用dismiss()函数时,它总是弹出回到根控制器,而不是列表视图。

这是我的代码:

Root View Controller:

NavigationView {
            List {
                Section(header: Text("Sortiert nach \(sortStr)")) {
                        ForEach(dbModel.players) { player in
                            NavigationLink(destination: FeedbackListView(player: player)) {
                                PlayerListItemView(player: player)
                            }
                        }// ForEach
                        .onDelete(perform: delete)
                    }
            } // List
}

FeedbackListView

List {
            ForEach(dbModel.feedbacks) { feedback in
                FeedbackListItemView(feedback: feedback)
            }
            .onDelete(perform: delete)
        }//List
        .listStyle(GroupedListStyle())
        
        .toolbar(content: {
            ToolbarItem(placement: .navigationBarTrailing) {
                HStack(spacing: 16) {
                    
                    NavigationLink(destination: AddNewFeedbackView(forPlayer: player)) {
                        ToolbarPlusButton()
                    }//Add Button
                }
            }
        }) // Toolbar

AddNewFeedbackView

Form {
            Section(header: Text("Feedback für \(forPlayer.fullName)")) {
                TextField("Thema", text: $topic)
            }
            
            Section(header: Text("Inhalt")) {
                TextEditor(text: $coachingCue)
                    .frame(minHeight: 120)
            }
            
            Section(header: Text("Wichtigkeit")) {
                Picker(selection: $rating, label: Text("Wichtigkeit")) {
                    ForEach(0..<pickerContent.count) {
                        Text(self.pickerContent[$0])
                    }
                }//Picker
                .pickerStyle(SegmentedPickerStyle())
            }
            
            Section(header: Text("Datum")) {
                DatePicker("", selection: $date, in: ...Date())
                    .labelsHidden()
            }
            
            Section {
                Button(action: {
                    let secondsDate = date.timeIntervalSince1970
                    let newFeedback = Feedback(id: "", date: secondsDate, player: forPlayer.id!, rating: rating, topic: topic, coachingCue: coachingCue)
                    dbModel.addFeedback(newFeedback)
                    self.presentationMode.wrappedValue.dismiss()
                }) {
                    Text("Save")
                }
            }
        }
        .navigationBarTitle("Neues Feedback", displayMode: .inline)

点击“保存”按钮时,我将被发回根控制器,而不是反馈列表视图。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的事情。问题的根源在于 NavigationLinkAddNewFeedbackView 位于 ToolbarItem 中。为什么这是个问题?我不知道。

但在知道这一点后,我发现了以下 https://www.hackingwithswift.com/forums/swiftui/unexpected-behaviour-with-toolbar-and-navigation-bar/4893。此处指出,一旦您将以下导航视图样式放在 NavigationView 上,它就会解决。

.navigationViewStyle(StackNavigationViewStyle())

所以在你的例子中它会导致:

NavigationView {
    List {
        Section(header: Text("Sortiert nach \(sortStr)")) {
            ForEach(dbModel.players) { player in
                NavigationLink(destination: FeedbackListView(player: player)) {
                    PlayerListItemView(player: player)
                }
            } // ForEach
            .onDelete(perform: delete)
        }
    } // List
}
.navigationViewStyle(StackNavigationViewStyle())