Home > Enterprise >  Present alert on previous screen SwiftUI
Present alert on previous screen SwiftUI

Time:10-10

I have a view that gives users the ability to select to go to an external support page or an in-app feedback screen.

struct HelpView: View {
    var body: some View {
        ZStack {
            Color("Tope")
                .ignoresSafeArea()
            
            List {
                NavigationLink(destination: FeedbackView()) {
                    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")
    }
}

The feedback screen is just a simple form that gives users the ability to fill out a text field and submit it to a backend. Once a user submits this field successfully, I present them a PopupView thanking them for their feedback. I would like to instead take them back to a screen, and present the alert of that screen, the help screen, instead. How can I achieve this? I thought about using a completion but was unsure of how to do so in this instance.

Here is the form I currently have in the FeedbackView

Button(action: {
    fvm.submitFeedback(parameters: ["description": feedback, "user_id": 
        session.userObject.id!]) {
            response, error in
                if error != nil {
                    print("error: \(response)")
                } else {
                // dismiss current screen
                presentationMode.wrappedValue.dismiss()
                // also present an an alert view on the previous screen somehow
            }
        }
    }, label: {
        Text("Send")
            .fontWeight(.semibold)
    })
.disabled(fieldHasText())

CodePudding user response:

you could try this approach using a simple binding:

import SwiftUI
import PopupView

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView() 
        }
    }
}
struct ContentView : View {
    var body: some View {
        NavigationView {
            HelpView()
        }
    }
}
struct HelpView: View {
    @State var showPopup = false  // <-- here
    
    var body: some View {
        ZStack {
            Color.pink.ignoresSafeArea()
            List {
                NavigationLink(destination: FeedbackView(showPopup: $showPopup)) {  // <-- here
                    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)
                }
            }
        }
    .popup(isPresented: $showPopup, autohideIn: 3) {
        Text("THANK YOU")
            .frame(width: 200, height: 60)
            .background(Color(red: 0.85, green: 0.8, blue: 0.95))
            .cornerRadius(30.0)
    }
        .listStyle(InsetGroupedListStyle())
        .navigationBarTitle("Help")
    }
}
struct FeedbackView : View {
    @Environment(\.dismiss) var dismiss
    @Binding var showPopup: Bool  // <-- here
    
    var body: some View {
        Button(action: {
            showPopup = true  // <-- here
            dismiss()
            }, label: {
                Text("Send").fontWeight(.semibold)
            })
    }
}
  • Related