SwiftUI - Changing Binding Attribute causes Screen Dismissing

88 Views Asked by At

I Pushed a PctView into NavigationView. When I change a binding value in PctView, it is dismissing itself. Please help understanding this behaviour.

import SwiftUI
struct Technology: Identifiable {
  var id: UUID = UUID()
  var techName: String
  var completionPct: Float = 0.0
}
struct parentView: View {
  @State var technology = Technology(techName: "Front End")
  @State var tempList = [1,2]
  var body: some View {
    NavigationView {
      VStack {
        NavigationLink {
          ChildView(technology: $technology)
        } label: {
          Text(technology.techName)
        }
      }.navigationTitle(technology.techName)
    }
  }
}
struct ChildView: View {
  @Binding var technology: Technology
  var body: some View {
    VStack {
      Text(technology.techName)
      NavigationLink {
        PctView(completionPct: $technology.completionPct)
      } label: {
        Text("Go To Pct Changing View")
      }.navigationTitle(technology.techName)
    }
  }
}
struct PctView: View {
  @Binding var completionPct: Float
  var body: some View {
    Button("Change Pct") {
      completionPct = completionPct + 0.1
    }.navigationTitle("Pct View")
  }
}
struct Test: PreviewProvider {
  static var previews: some View {
    parentView()
  }
}
1

There are 1 best solutions below

0
malhal On

You can't have 2 levels of NavigationLink with NavigationView by default. You need to either set the second link to .isDetail(false) or you can make SwiftUI generate a UINavigationController instead of a UISplitViewController by using .navigationStyle(.stack). Best only do this if you are not targeting iPad.