skip to Main Content

On appear Modifier wont trigger listener on first view appearance.
When I sign into the application and click the appropriate tab view it does not trigger the on appear… it only triggers it when I click it twice…It works in all other situations but when I sign in for the first time it does not trigger when I click the Conversationlistview Tab.

ContentView {
        TabView(selection: $selectedTab) {
            FriendsView(selectedTab: $selectedTab, cameFromConversationsListView: true)
                .tabItem {
                    Label("Friends", systemImage: "message")
                }
                .tag(Tab.friends)
               
            ConversationsListView(selectedTab: $selectedTab, cameFromConversationsListView: true)
                .tabItem {
                    Label("Messages", systemImage: "person.fill")
                }
                .tag(Tab.Messages)
                .onAppear {
                  setupInitialListeners()
                }
           
          
        
        SettingsView()
            .tabItem {
                Label("Settings", systemImage: "gearshape")
            }
            .tag(Tab.settings)
            
         


       }

}
......
struct ConversationsListView: View {

var body: some View {
                  Vstack{
  //OTHER CONTENT TO DISPLAY HERE
                        }
                        .onAppear(perform: {
                            DispatchQueue.main.async {
                                
                                setupInitialListeners()
                            }
                            
                        })
}
}.....

I have tried placing the setupInitialListener in a ViewModel then calling it as ViewModel.setupInitialListener. I placed the on appear on the tabview. I placed it in the sign in function when I sign in to the app initially. I tried calling it from the @main Delegate. I also created a custom tab format with navigation links. I’m not sure what to do.

2

Answers


  1. Chosen as BEST ANSWER

    I got it to work and finally call the listener... strangely enough it only works when I use...

           .onDisappear {
                            DispatchQueue.main.async {setupListeners()}
    
    
    
     } .onAppear { DispatchQueue.main.async { setupListeners() }
    }
    

    and put the same call for the listener in both functions...when I tried to call it individually in either disappear or appear it wont trigger... But when using both with on disappear above the appear it works. I have no idea why but I played around and tried this and now it does work.. maybe this will help someone in the future. Thanks allot for your responses Jayant. You gave me a reason to keep trying. All the best.


  2. Add the body in your ContentView and remove the setupInitialListeners() from two places. Either put it inside the ConversationsListView or ContentView, and it will work fine as expected. When you tap on Messages tab, the setupInitialListeners() function will be called when the view appears. Here is my code, which is working fine.

    and Please provide minimal reproducible code next time

    import SwiftUI
    
    enum Tab {
        case friends
        case Messages
        case settings
    }
    
    struct ContentView: View {
        @State private var selectedTab: Tab = .friends
        
        var body: some View {
            TabView(selection: $selectedTab) {
                
                FriendsView(selectedTab: $selectedTab, cameFromConversationsListView: true)
                    .tabItem {
                        Label("Friends", systemImage: "message")
                    }
                    .tag(Tab.friends)
                
                ConversationsListView(selectedTab: $selectedTab, cameFromConversationsListView: true)
                    .tabItem {
                        Label("Messages", systemImage: "person.fill")
                    }
                    .tag(Tab.Messages)
                    .onAppear {
                        setupInitialListeners()
                    }
                
                SettingsView()
                    .tabItem {
                        Label("Settings", systemImage: "gearshape")
                    }
                    .tag(Tab.settings)
            }
        }
        
        func setupInitialListeners() {
            print("setupInitialListeners tapped")
        }
    }
    
    struct FriendsView: View {
        @Binding var selectedTab: Tab
        var cameFromConversationsListView: Bool
        
        var body: some View {
            Text("Friends View")
        }
    }
    
    struct ConversationsListView: View {
        @Binding var selectedTab: Tab
        var cameFromConversationsListView: Bool
        
        var body: some View {
            VStack {
                Text("Conversations List View")
            }
        }
    }
    
    struct SettingsView: View {
        var body: some View {
            Text("Settings View")
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search