0

My swiftui application structure looks like this

  • Navigation View (enclosing the landing view that is a list view )
  • On selection of a List item Navigation link directs to a Tab View with three tabs (default first tab)

When I use a sole standalone navigation link inside tab view screens to direct to another screen programatically, it navigates succesfully to the mentioned destination, but my binding doesn't work to come back to the previous screen.

Parent View

@State var showCameraPreviewView : Bool = false 
ZStack{
 Button("Show camera") {
  showCameraPreviewView = true
}
 NavigationLink(destination: CameraView(showCameraPreviewView: $showCameraPreviewView),isActive: $showCameraPreviewView){
                 EmptyView()
             }
}

Child View

@Binding var showCameraPreviewView 

Button("Assume capture success"){
   self.showCameraPreviewView = false
}

Toggling showCameraPreviewView binding to false in the destination doesn't get me back to the current screen. Looks straight forward, but doesn't work ! anything that I'm doing wrong ?

  • its hard to tell from the partial code you are sharing. I don't see where and how you reset `showCameraPreview` back to false. You could use a sheet for the cameraView. – ChrisR Feb 27 '22 at 15:35
  • @ChrisR updated the child view in the question, it's kind of straight forward Binding update. Yea sheet works fine but any thoughts on why the above is not working ? – Fareed khan Feb 27 '22 at 16:52

1 Answers1

0

I can reproduce your issue, quite strange ... seems like the change of showCameraPreviewView is not accepted because the view is still visible. But I found a workaround with dismiss:

EDIT for iOS 14:

struct ChildView: View {
    
    @Environment(\.presentationMode) var presentationMode

    @Binding var show: Bool
    
    var body: some View {
        Button("Assume capture success"){
            show = false
            presentationMode.wrappedValue.dismiss()
        }
    }
}
ChrisR
  • 9,523
  • 1
  • 8
  • 26
  • dismiss is only available on ios 15+ , any other alternative ? By the way I tried using the @Environment(\.presentationMode) var presentationMode and presentationMode.wrappedValue.dismiss() , but this takes me back to the root navigation view and not the immediate previous screen – Fareed khan Feb 27 '22 at 17:35
  • hmmm ... `presentationMode.wrappedValue.dismiss()`works fine with me as well, but I'm on iOS15.2. What's wrong with iOS15 – or what version are you targeting? – ChrisR Feb 27 '22 at 17:39
  • targeting ios 14+ for my app – Fareed khan Feb 27 '22 at 17:40
  • ok, edited my code. The combination seems to work for iOS 14 – dont know why. – ChrisR Feb 27 '22 at 19:04