SwiftUI实现提交反馈后返回上一页面并展示提示框
Solution: Show Alert on HelpView After Feedback Submission
Hey, let's get this sorted out for you! Using a completion closure is exactly the right approach here—here's how to implement it step by step:
Step 1: Add a Completion Closure to FeedbackView
First, we'll add an optional closure property to FeedbackView that triggers when feedback is successfully submitted. This creates a way for the view to notify HelpView that the action is done.
Update your FeedbackView like this:
struct FeedbackView: View { // Add this closure to notify the parent view of successful submission var onFeedbackSubmitted: (() -> Void)? // Keep your existing properties (presentationMode, fvm, feedback, session, etc.) @Environment(\.presentationMode) var presentationMode // ... your other existing properties var body: some View { // ... your existing view content Button(action: { fvm.submitFeedback(parameters: ["description": feedback, "user_id": session.userObject.id!]) { response, error in if error != nil { print("error: \(response ?? "No response data")") } else { // Trigger the closure to inform HelpView submission succeeded onFeedbackSubmitted?() // Dismiss the FeedbackView presentationMode.wrappedValue.dismiss() } } }, label: { Text("Send") .fontWeight(.semibold) }) .disabled(!fieldHasText()) // Note: Adjust this if your fieldHasText() logic needs correction } }
Step 2: Update HelpView to Handle the Closure and Show Alert
Next, we'll add a state variable to HelpView to control when the success alert appears. We'll pass a closure to FeedbackView that sets this state variable to true once feedback is submitted.
Update your HelpView:
struct HelpView: View { // State variable to toggle the success alert @State private var showSuccessAlert = false var body: some View { ZStack { Color("Tope") .ignoresSafeArea() List { // Pass the completion closure to FeedbackView NavigationLink(destination: FeedbackView(onFeedbackSubmitted: { // Trigger the alert when feedback is submitted showSuccessAlert = true })) { Text("Report a problem") .fontWeight(.semibold) } HStack { Link(destination: URL(string: "https://sauced.app")!) { Text("Help Center") .fontWeight(.semibold) } Spacer() Image(systemName: "chevron.right") .foregroundColor(Color.black) } } } .listStyle(InsetGroupedListStyle()) .navigationBarTitle("Help") // Add the success alert configuration .alert("Thanks for your feedback!", isPresented: $showSuccessAlert) { Button("OK", role: .cancel) {} } message: { Text("We've received your submission and will look into it shortly.") } } }
How This Works
- The
onFeedbackSubmittedclosure acts as a communication bridge betweenFeedbackViewandHelpView. When feedback is successfully sent,FeedbackViewcalls this closure right before dismissing itself. - The
showSuccessAlertstate variable inHelpViewcontrols the alert's visibility. When the closure is triggered, this variable switches totrue, making the alert pop up automatically. - The
.alertmodifier lets you customize the title, message, and button text to match your app's tone—feel free to tweak these values as needed.
内容的提问来源于stack exchange,提问作者user11371080
相关产品推荐
相关产品推荐

