Learn Courses My Dashboard

How to link 2 content views?

I’m trying to figure out how to combine some of my pages - so after you click a button, another screen pops up. I already have both of the screens coded, but can’t figure out how to link them together. Is there a video on how to do that? sorry if I sound dumb

@carolinekane

Hi Caroline,

Welcome to the community.

There is no such thing as a dumb question here. We all started out knowing little to nothing about programming and gradually learned.

When you say “can’t figure out how to link them together”, what is it that you are trying link from one screen to the other? Do you want data from one to be passed to the other?

Perhaps you could post your code that you have for each.

Paste your code in as text, rather than providing a screenshot.

To format the code nicely, place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code. Like this:

```
Code goes here
```

The 3 back-ticks must be the ONLY characters on the line. The back-tick character is located on the same keyboard key as the tilde character ~ (which is located below the Esc key - QWERTY keyboard).

Alternatively after you paste in your code, select that code block and click the </> button on the toolbar. This does the same thing as manually typing the 3 back ticks on the line above and below your code.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing.

1 Like

hi @carolinekane

Welcome to the community. :slight_smile:

In SwiftUI, you can use the NavigationStack view to create a button that, when tapped, will navigate to another screen.

Here is an example of how you can use NavigationLink to navigate to another screen when a button is tapped:

Here is the sample implementation

struct ContentView: View {
    var body: some View {
        NavigationStack {
            VStack {
                NavigationLink(destination: DetailView()) {
                    Text("Go to Detail View")
                }
            }
        }
    }
}

struct DetailView: View {
    var body: some View {
        Text("This is the detail view")
    }
}

In this example, when the “Go to Detail View” button is tapped, the user will be taken to the DetailView screen.

Also you can use .sheet Modifier and presentation to navigate between pages.

struct ContentView: View {
    @State private var showDetail = false
    var body: some View {
        Button("Show Detail") {
            self.showDetail.toggle()
        }.sheet(isPresented: $showDetail) {
            DetailView()
        }
    }
}

Please note that you need to wrap your views with a NavigationStack for NavigationLink to work properly.