TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Pop to Parent View Via Bar Button

Forums > SwiftUI

Hey, everyone!

I found out today that a problem that I (and many others) had had since switUI's release had been solved: popping to the parent view. (see the stackoverflow question here: https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view) However, when I implemented it into my app, it did not work. After a bit of testing, I found out that it was because the navigationLink was nested inside a navigationBarItem. Below is the edited code, which does not pop to the parent view.

A) Why is this? B) How can I fix it? I would prefer to keep using the navigationBarItem, but if it won't work then I may not have a choice. I've messed with a couple of other hacks from the above stackoverflow that work, but none are as elegant as this.

Code (tested in a playground, should work in a project):


import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    @State var isActive : Bool = false

    var body: some View {
        NavigationView {
            Text("hello")
                .navigationBarItems(trailing: 
                    NavigationLink(
                        destination: ContentView2(rootIsActive: self.$isActive),
                        isActive: self.$isActive
                    ) {
                        Text("Hello, World!")
                    }
                    .isDetailLink(false)
                )

            .navigationBarTitle("Root")
        }
    }
}

struct ContentView2: View {
    @Binding var rootIsActive : Bool

    var body: some View {
        VStack {
            NavigationLink(destination: ContentView3(shouldPopToRootView: self.$rootIsActive)) {
            Text("Hello, World #2!")

            }
            .isDetailLink(false)
            .navigationBarTitle("Two")
            Button (action: { self.rootIsActive = false } ){
                Text("Pop to root")
            }

        }
    }
}

struct ContentView3: View {
    @Binding var shouldPopToRootView : Bool

    var body: some View {
        VStack {
            Text("Hello, World #3!")
            Button (action: { self.shouldPopToRootView = false } ){
                Text("Pop to root")
            }
        }.navigationBarTitle("Three")
    }
}

PlaygroundPage.current.setLiveView(ContentView())
PlaygroundPage.current.needsIndefiniteExecution = true

2      

I found a solution to your problem: Instead of configuring the bar button itself as the navigation link, make it a simple button and have it toggle the isActive flag. Then, in your main body, insert a NavigationLink with an EmptyView() as its label and have it become active in response to the isActive flag being toggled to true by your bar button. Here's the code:

struct ContentView: View {
    @State var isActive : Bool = false

    var body: some View {
        NavigationView {
            VStack {
                Text("hello")
                NavigationLink(destination: ContentView2(rootIsActive: self.$isActive), isActive: $isActive) { EmptyView() }
                    .isDetailLink(false)
            }
            .navigationBarItems(trailing:
                Button(action: {
                    self.isActive.toggle()
                }, label: {
                    Text("Hello World")
                })

            )
                .navigationBarTitle("Root")
        }
    }
}

4      

Thanks so much!

This works exactly as I want it to.

(By the way, also tested in a full-on Xcode project)

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.