Swift + OS X + Xcode : Change NavigationLink destination and text programtically

Hello,

I am trying to learn Swift. I have the code below. With a button click I can move to a new view. What I would like to do is once the new view is displayed have the navigation link destination changed to "CurrentView" and the button text to "Previous View".
Any insight on how to resolve this issue would be appreciated.

Regards,

Chris

import SwiftUI struct ContentView: View {     var body: some View {         NavigationView {             NavigationLink(destination: NextContentView()){                 Text("Next View")             }         }     } }

You should better use Code Block properly. More readable code would appeal to more readers.

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: NextContentView()){
                Text("Next View")
            }
        }
    }
}

Declare a state var and have a test:

import SwiftUI

struct ContentView: View {
    @State var firstPass: Bool = true  

    var body: some View {
        NavigationView {
            if firstPass  {
               NavigationLink(destination: NextContentView()){ Text("Next View") }
            } else {
               NavigationLink(destination: CurrentView()){ Text("Other View") }
            }
        }
    }
}

Thank you for the response but the code appears to only take me to the second view. The button text does not change and it does not take me back to the first view. Does the var firstPass have to be changed to false once the NextContentView() is displayed?

Chris

After some additional testing, it appears that when an @State variable changes the view is redrawn. So it would appear that all I need to do is toggle firstPass when the navigation button is selected. Unfortunately I have yet to find a way to do that. If anyone knows, please let me know.

Chris

Claude,

I have attempted to add a line beneath navigation link as follows: self.firstPass.toggle()

It does not work. Any ideas. Sorry I overlooked you last response.

Chris

I have looked at using observable objects and adjusted my code as shown below. Xcode does not like any line with the word ObservableObject in it. The attempt I am trying to make is to change the firstPass variable so that the navigation panel in ContentView redraws. I was going also link the observable object to the NextContentView view, toggle it there then hope that the ContentView navigation panel finally redraws and shows the updated button text and NavigationLink destination.

My hope is that I have made some simple stupid error.

Any help would be appreciated.

Chris

import SwiftUI

import Combine

class SelectedView: ObservedObject {

    @Published var firstPass : Bool = true

}

struct ContentView: View {

    

    @ObservedObject var theSelection = SelectedView()

    

    var body: some View {

        NavigationView {

            if (self.theSelection.firstPass){

                NavigationLink(destination: NextContentView()){

                    Text ("Next View")

                }

            }

            else {

                NavigationLink(destination: ContentView()) {

                    Text ("Previous View")

                        }

                }

            }

    }

}

Swift + OS X + Xcode : Change NavigationLink destination and text programtically
 
 
Q