5

The code below was working (onAppear was getting called) but as soon as I nested my two views inside a tabView (to present a UIPageViewController-like UI) the second view's onAppear function never gets called, is this a bug or something I'm doing wrong?

struct SetUpWorkoutView: View {
        
        var body: some View {
            NavigationView {
                List {
                    ForEach(sessionTypes) { sessionType  in
                    NavigationLink(destination: LiveWorkoutView(sessionType: sessionType)) {
                        SessionTypeRow(name: sessionType.stringValue)
                    }
                }
            }
        }


 struct LiveWorkoutView: View {
     var body: some View {
    VStack(alignment: .leading) { }
      }.onAppear(perform: {
                    print("on appear called in LiveWorkoutView")
      }
    }
   }
}

@main
struct SwiftUITestApp: App {

    var body: some Scene {
        WindowGroup {
            TabView {
                SetUpWorkoutView()
                WatchSettingsView()
            }
            .tabViewStyle(PageTabViewStyle())
        }
    }
}
GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • 1
    Try wrapping your two views (SetupWorkout and Watchsettings) in NavigationViews WITHIN your @main app. Then *remove* the Navigation view wrappers in the views themselves. I can’t promise this will work, but in my experience the tab view structure worked better if the NavView wrappers were writer there at the level of the tab view. – fronesis Aug 14 '20 at 22:57
  • Works fine with Xcode 12 / iOS 14 – Asperi Aug 15 '20 at 04:09
  • @Asperi I'm seeing this in Xcode 12 Beta 4 – GarySabo Aug 16 '20 at 13:21
  • 1
    @fronesis this does seem to work! I'm not sure why but doing this makes .onappear now get called in LiveWorkoutView – GarySabo Aug 16 '20 at 13:26
  • I have Xcode 12b3 – Asperi Aug 16 '20 at 13:26
  • @fronesis This is the correct answer. I couldn’t find anything helpful anywhere else, but this solution fixed my similar issue. – jh95 Feb 11 '21 at 17:49

0 Answers0