UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Pop up sheet in child view automatically closing and navigating back to parent view

Forums > SwiftUI

Hello all,

I am at my wits end with this bug and it is one of the last things in my project. I'm sure a feeling all y'all have felt.

I have the issue of when passing data to my child view using observedObjects, the sheet in my child view opens and closes immediately and navigates back to the parent view in its original state. The sheet I am refering to is opens when the button with the scribble symbol is clicked. This is an attempted yelp clone to some extent. Any help would be much appreciated. Thank you very much.

If someone can give me a solution that works in the context of my code, I will send you $20 in BTC

struct worldView: View {

    @State private var isShowing = false
    @StateObject var viewModel2 = worldViewModel()
    @ObservedObject var viewModelHome: homeScreenViewModel

    init() {
        self.viewModelHome = homeScreenViewModel(city1: "Athens")
    }

    var body: some View {
        NavigationView {

            ZStack {
                if isShowing {
                    sideMenuView(isShowing: $isShowing)
                }
                VStack {
                    HStack{
                        Button(action: {
                            withAnimation(.spring()) {
                                isShowing.toggle()
                            }
                        }, label: {
                            Image(systemName: "line.3.horizontal")
                                .resizable()
                                .frame(width: 25,height: 25, alignment: .leading)
                                .foregroundColor(.blue)

                        })

                        VStack{
                            HStack {

                                Menu {
                                    ForEach(viewModel2.queriedCities, id: \.self) { city in
                                        Button(action: {
                                            viewModel2.setCity(location: city)
                                            viewModelHome.loadPlaces(location: city)
                                        }, label: {
                                            Text(city)
                                        })
                                    }
                                } label: {
                                    Image(systemName: "location.magnifyingglass")
                                        .resizable()
                                        .frame(width: 25,height: 25, alignment: .leading)
                                        .foregroundColor(.blue)
                                }
                            }
                        }

                    }

                    TabBar(viewModel: viewModel2, viewModelHome: viewModelHome)
                        .navigationBarTitleDisplayMode(.inline)
                }

            }
            .frame(maxWidth: .infinity,maxHeight: .infinity)
        }.navigationBarBackButtonHidden(true)
            .onAppear{
                viewModelHome.loadPlaces(location: "Athens")
            }
            .navigationViewStyle(StackNavigationViewStyle())
    }

}

struct worldView_Previews: PreviewProvider {
    static var previews: some View {
        worldView()
    }
}

extension worldView {

}

struct TabBar: View {
    @ObservedObject var authViewModel = AuthenticationViewModel()

    @ObservedObject var viewModel: worldViewModel

    @ObservedObject var viewModelHome: homeScreenViewModel

    var body: some View {
        VStack(spacing: 0.0) {
            TabView {
                if let user = authViewModel.currUser {

                    HomeScreen(viewModelHome: viewModelHome, cityViewModel: viewModel)
                        .tabItem {
                            Image(systemName: "globe.europe.africa")
                        }
                    profileView(user: user)
                        .tabItem {
                            Image(systemName: "person.crop.square")
                        }

                } else{
                    EmptyView()
                }
            }
            .toolbarColorScheme(.dark, for: .tabBar)
        }.navigationBarBackButtonHidden(true)
        .ignoresSafeArea(.all)
        .frame(maxWidth: .infinity,maxHeight: .infinity)
    }
}
struct HomeScreen: View {

    @State var keyword = ""
    @State var places: [Place] = []
    @ObservedObject var viewModelHome: homeScreenViewModel
    @ObservedObject var cityViewModel: worldViewModel

    init(viewModelHome: homeScreenViewModel, cityViewModel: worldViewModel) {
        self.viewModelHome = viewModelHome
        self.cityViewModel = cityViewModel    
    }

    var body: some View {

        VStack{

            NavigationView {

                ScrollViewReader { proxyReader in
                    ScrollView(.vertical, showsIndicators: false, content: {
                        ForEach(viewModelHome.places) {place in
                            NavigationLink(destination: {
                                BioView()}, label: {
                                    Post(viewModel: viewModelHome, place1: place, viewModel1: postViewModel(place1: place))

                                })
                        }

                    })

                }

            }

        }
    }

}
struct BioView: View {

    @State var scrollViewOffset: CGFloat = 0
    @State private var showSheet = false
    @ObservedObject var viewModel: BioViewModel
    @Environment(\.dismiss) private var dismiss

    init() {
        self.viewModel = BioViewModel()
    }

    var body: some View {
        ScrollViewReader {proxyReader in
            ScrollView {
                VStack {
                    HStack {
                        Button {
                            // 2
                            dismiss()

                        } label: {
                            HStack {
                                Image(systemName: "arrowshape.backward.fill")
                                    .resizable()
                                    .foregroundColor(Color("Color 1"))
                                    .padding(.leading)
                                    .frame(width: 40,height: 17)
                            }
                        }
                        Spacer()
                    }

                        VStack {
                           Text("g")
                        }
                        .overlay(
                            Button(action: {
                                showSheet = true
                            }, label: {
                                Image(systemName: "scribble.variable")
                                    .font(.system(size: 20, weight: .semibold))
                                    .foregroundColor(.white)
                                    .padding()
                                    .background(Color("Color 2"))
                                    .clipShape(Circle())
                                    .shadow(color: Color.black.opacity(0.09), radius: 5, x: 5, y: 5)

                            })
                            .sheet(isPresented: $showSheet, onDismiss: {print("dismissed")}, content: {newCommentView(location: place.name) })

                            ,alignment: .bottomTrailing
                        )
                    }
                }
                .padding(.bottom, 30)
                .padding(.top, 20)
            }
            .navigationBarBackButtonHidden(true)
            .padding(.horizontal, 20)
        }
    }

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.