0

I keep receiving errors in my debugger saying:

2020-07-18 15:59:07.256759-0400 ThePillowApp[22009:7205609] Warning: Attempt to present <SwiftUI.PlatformAlertController: 0x1070aba00> on <TtGC7SwiftUI19UIHostingControllerVVS_22_VariadicView_Children7Element: 0x106b978e0> whose view is not in the window hierarchy!

A snapshot of my code:

    ##First View    
    class User: ObservableObject {
            
            @Published var test = false
            
        }
        
        struct RegisterView: View {
            
            @EnvironmentObject var user: User
        
        var body: some View {
                NavigationView{
...
                    HStack{
                        NavigationLink(destination: AccountView().navigationBarTitle("")
                            .navigationBarHidden(true), isActive: self.$user.test) {
                            Text("").foregroundColor(.black).font(Font.custom("DiariaPro-Light", size: 12))}
                        
                        Button(action: {self.user.test=true}) {
                                    Text("Don't have an account?")
                            .font(Font.custom("DiariaPro-Light", size: 12))
                            .foregroundColor(.black)
                        }}}

##Second View
struct AccountView: View {
    @EnvironmentObject var user: User
    @State private var alertMessage = "Error message"
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    
var body: some View {
        NavigationView{
            ...
                        HStack{
                            
                            
Button(action: {
    
    DispatchQueue.main.async {
    self.user.test=false
    self.presentationMode.wrappedValue.dismiss()

    }
}) {
            Text("Already have an account?")
    .font(Font.custom("DiariaPro-Light", size: 12))
    .foregroundColor(.black)
}}

Basically, what I want to do is on the first view, if you don't have your account, you click the button which takes you to the sign up page. On the sign up page (#second view), you can create an account. The problem I am running into is that it appears that after I dismiss the second view (via self.user.test=false or self.presentationMode.wrappedValue.dismiss()) and return to the first view, the second view is still presenting itself.

I know this because I installed alerts on both views, and when I triggered the alert on the first view, there was no error about "the platform alert controller" trying to present on the hosting controller. On the second view, that error was there (I presume because I navigated to the second view from the first view). BUT when I go from the first to the second and back to the first, that warning remains, which is the one about presenting (even after i dismissed).

  • Hi, maybe I don't anderstand the question but the error says you are trying to present the alert on a view that is NOT in the window hierarchy... this means the second view is actually dismissed. I don't know the 'PlatformAlertController' but a normal alert windows needs to be presented on the view actualy shown. – Alrik Jul 18 '20 at 20:55
  • Thanks Alrik. This is the confusing part. I am using this alert below embedded to my ZStack `.alert(isPresented: self.$user.failed) { Alert(title: Text(self.alertMessage), message: nil, dismissButton: .default(Text("Try again")) { self.user.failed = false })` From First Screen > Go to Second Screen Via Navigation Link. If i bring up the alert on the 2nd screen, I get that warning. ALSO, if i dismiss second screen and go to first screen, and trigger alert, I also get that warning. – ThePillowCo Jul 18 '20 at 21:26
  • Ok, i'm learning like you, so should say something wrong... in that case, sorry. The second event make sense: you dismiss the second view so the alert can't be shown. The first event seems to fire "before" the second view is shown. In the code you reported there is no hint of the ZStack that should contain both view to come in handy. Could you please add it to the question? – Alrik Jul 19 '20 at 16:54

0 Answers0