pelli23
pelli23

Reputation: 167

NavigationLink, isActive doesn´t work in SwiftUI

I´m trying to programmatically change the current view to other, but isActive attribute from NavigationLink is not working, I guess that I´m forgeting something.

struct MainView: View {
    @State public var pushActive = true

    var body: some View {
        NavigationView{
        Text("hello")
        NavigationLink(destination: ContentView(), isActive: $pushActive) {
            Text("")

        }.hidden()
        }.onAppear{
            self.pushActive = true

        }
    }

}

This view always show "hello" instead of redirect to ContentView

Upvotes: 2

Views: 5215

Answers (2)

Arutyun Enfendzhyan
Arutyun Enfendzhyan

Reputation: 1909

NavigationView accepts only the first child, that's why it wasn't working. Try this:

    struct MainView: View {
        @State public var pushActive = true

        var body: some View {
            NavigationView {
                VStack {
                    Text("hello")
                    NavigationLink(destination: ContentView(), isActive: $pushActive) {
                        Text("")
                    }.hidden()
                }
            }.onAppear {
                self.pushActive = true
            }
        }
    }

Upvotes: 4

pelli23
pelli23

Reputation: 167

I solved the problem, The error was put the Text("hello"). The answer is simple:

 NavigationView{
            if(pushActive){
                NavigationLink(destination: ContentView(), isActive: $pushActive) {
                    Text("")

                }.hidden()
            }else{
                Text("hello")
            }

Upvotes: 1

Related Questions