You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 onFeedbackSubmitted closure acts as a communication bridge between FeedbackView and HelpView. When feedback is successfully sent, FeedbackView calls this closure right before dismissing itself.
  • The showSuccessAlert state variable in HelpView controls the alert's visibility. When the closure is triggered, this variable switches to true, making the alert pop up automatically.
  • The .alert modifier 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 18:02:45