0

I have a watchOS App which uses the following layout:

NavigationView {
    if !HKHealthStore.isHealthDataAvailable() {
        ContentNeedHealthKitView()
    } else if !isAuthorized {
        ContentUnauthorizedView()
    } else {
        TabView(selection: $selection) {
            WeightView()
                .navigationTitle("Weight")
                .tag(1)
                .onAppear {
                    print("Appear!")
                }
                .onDisappear {
                    print("Disappear!")
                }
            SettingsView()
                .navigationTitle("Settings")
                .tag(2)
        }
    }
}

Unfortunately, the OnAppear and OnDisappear actions are only executed after transitioning from on view to another the second time. When swiping right the first time, nothing happens.

LuMa
  • 1,673
  • 3
  • 19
  • 41

1 Answers1

0

You should provide a minimal reproducible example (see https://stackoverflow.com/help/minimal-reproducible-example).

Also your lines are producing compiler errors. The correct way to use onAppear is like this:

.onAppear {

}

Here is a working example, everything is working as expected. You should also place the onAppear ViewModifier to the child view.

import SwiftUI

struct WeightView: View {
    var body: some View {
        Text("WeightView")
         .onAppear {
            print("Appear!")
         }
         .onDisappear {
            print("Disappear!")
         }
    }
}

struct SettingsView: View {
    var body: some View {
        Text("SettingsView")
    }
}

struct ContentView: View {
    @State var selection = 1
    @State var isAuthorized = false

    var body: some View {
        NavigationView {
            if !isAuthorized {
                Button("authorize") {
                    isAuthorized.toggle()
                }
            } else {
                TabView(selection: $selection) {
                    WeightView()
                        .navigationTitle("Weight")
                        .tag(1)
                    SettingsView()
                        .navigationTitle("Settings")
                        .tag(2)
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Ali
  • 761
  • 1
  • 5
  • 24