1

ContentView.swift

  TabView{
        RoomListView(myRoom: $viewModel.rooms)
            .onAppear {
                viewModel.populateRoomList()
                viewModel.roomJoinRequestUpdate()
            }
            .tabItem {Label("Rooms", systemImage: "house.fill")}
                                 

I visit multiple views using NavigationLink inside RoomListView. How do I come back to RoomListView by pressing the tabItem linked to it?

1 Answers1

0

My Solution: (Reference)

 @State private var roomList = UUID()
    @State private var tabSelected = 1
    @State private var tappedTwice: Bool = false
    
    var handler: Binding<Int> { Binding(
                        get: { self.tabSelected },
                        set: {
                            if $0 == self.tabSelected {
                                // Lands here if user tapped more than once
                                tappedTwice = true
                            }
                            self.tabSelected = $0
                        }
                )}
    
    var body: some View {
        
     
            
        if currentPage > totalPages {
            ZStack(alignment: .topLeading) {
                Group {
                    if viewModel.userSession == nil {
                        LoginView().environmentObject(viewModel)
                           
                    } else {
                        TabView(selection: handler){
                            RoomListView(myRoom: $viewModel.rooms).id(roomList)
                                .onChange(of: tappedTwice, perform: { tappedTwice in
                                                                           guard tappedTwice else { return }
                                                                             roomList = UUID()
                                    self.tappedTwice = false
                                                                   })
                                .onAppear {
                                    viewModel.populateRoomList()
                                    viewModel.roomJoinRequestUpdate()
                                }
                                .tabItem {Label("Rooms", systemImage: "house.fill")}
                               .tag(1)