Home > Blockchain >  SwiftUI Navigation immediately dismisses views? iOS 15
SwiftUI Navigation immediately dismisses views? iOS 15

Time:02-21

I am creating an edit account info navigation from one of my apps main views but when I push the 3rd or 4th view onto the stack, that view is immediately dismissed????

Sometimes Child B will stay on the stack and sometimes it doesn't, but Child C views are always dismissed immediately and there is no code inside those views that would cause that behavior...???

Below is the navigation stack diagram.

                               (auto dismissed)    (auto dismissed)
  Parent         CHILD A            CHILD B            CHILD C 
ProfileView -> SettingsMenu.   ->.  Account      ->   EditEmail

The problem I am having is that any view I navigate to from Child B is immediately dismissed so the user is unable to edit their e-mail, phone-number, or gender..

I cannot figure out what is wrong because I am storing all of my isActive properties as @State variables in each view to programmatically navigate to the corresponding view??

Any ideas what is wrong??

Profile (PARENT)

struct ProfileView: View {
    @EnvironmentObject var session: SessionStore
    @StateObject var profileVM = ProfileViewModel()

    @State private var presentSettings: Bool = false

    var body: some View {
        NavigationView {
            List {
               //other content
                Text("Show settings menu").onTapGesture { presentSettings = true }
            } 
            .listStyle(PlainListStyle())
            .navigationViewStyle(StackNavigationViewStyle())
            .background (
                NavigationLink("", destination: Settings(), isActive: $presentSettings)
            )
        }
    }
}

Child A navigation view activated from PARENT

struct Settings: View {
    @EnvironmentObject var session: SessionStore
    @Environment(\.presentationMode) var presentation
    
    @State private var presentAccount: Bool = false
    @State private var presentSecurity: Bool = false
    @State private var presentNotification: Bool = false
    
    var body: some View {
            VStack(spacing: 30) {
                AccountMenuTab()
                    .onTapGesture { presentAccount = true }
  
                SecurityMenuTab()
                    .onTapGesture { presentSecurity = true }
          
                NotificationTab()
                    .onTapGesture { presentNotification = true  }
             
            }
       // THESE VIEWS GET IMMEDIATELY DISMISSED ON NAVIGATION 
        .background ( NavigationLink("", destination: AccountTab(), isActive: $presentAccount))
        .background ( NavigationLink("", destination: SecuritySettings(), isActive: $presentSecurity))
        .background ( NavigationLink("", destination: UserNotificationsView(), isActive: $presentNotification))
    }
}

Child B navigation activated from Child A Any navigation to another view from this view (edit Email, Phone, or Gender) are immediately dismissed???

struct AccountTab: View {
    @EnvironmentObject var session : SessionStore
   
    @StateObject var accountVM = AccountTabViewModel()

    @State private var editEmail: Bool = false
    @State private var editPhone: Bool = false
    @State private var editGender: Bool = false

    var body: some View {
        VStack {
            Text("Email").onTapGesture { editEmail.toggle() }
            Text("Phone").onTapGesture { editPhone.toggle() }
            Text("Gender").onTapGesture { editGender.toggle() }
        }
        // BELOW VIEWS GET IMMEDIATELY DISMISSED AFTER NAVIGATION??
        .background ( NavigationLink("", destination: EditEmail(), isActive: $editEmail) )
        .background( NavigationLink("", destination: EditGender(), isActive: $editGender) )
        .background( NavigationLink("", destination: EditPhone(), isActive: $editPhone) )
           
    }
}

CodePudding user response:

No need for .onTapGesture or .background ...
It should look something like this:

struct ProfileView: View {
    
    var body: some View {
        NavigationView {
            List {
                Text("other content ...")
                //other content {
                NavigationLink("Show settings menu") {
                    Settings()
                }
            }
            .listStyle(PlainListStyle())
            .navigationViewStyle(StackNavigationViewStyle())
        }
    }
}


struct Settings: View {
    
    var body: some View {
        VStack(spacing: 30) {
            
            NavigationLink("Account Menu") {
                AccountTab()
            }
            NavigationLink("Security Menu") {
//                SecuritySettings()
            }
            NavigationLink("Notification Menu") {
//                UserNotificationsView()
            }
        }
    }
}


struct AccountTab: View {
        
    var body: some View {
        VStack {
            NavigationLink("Email") {
//                EditEmail()
                Text("Edit Mail")
            }
            NavigationLink("Phone") {
//                EditGender()
                Text("Edit Phone")
            }
            NavigationLink("Gender") {
//                EditPhone()
                Text("Edit Gender")
            }
        }
    }
}
  • Related