# SwiftUI: the NavigationView view

> Learn how to use NavigationView in SwiftUI to set a navigationTitle and let users tap a NavigationLink to move from one view to another in your app.

Author: Flavio Copes | Published: 2021-10-03 | Canonical: https://flaviocopes.com/swiftui-navigationview/

The `NavigationView` view is a very important view, and one you'll use all the time.

```swift
NavigationView {

}
```

Once you wrap a view into a `NavigationView`  you can add a title to the view with the `navigationTitle()` modifier:

```swift
NavigationView {
    Text("Hello")
        .navigationTitle("Welcome")
}
```

![SwiftUI NavigationView with Welcome title and Hello text displayed in iPhone simulator](https://flaviocopes.com/images/swiftui-navigationview/Screen_Shot_2021-09-23_at_18.10.58.png)

The main benefit, however, is that now we can make views be links that bring the user to other views.

First thing we do is creating another view. You an add it to the same file, or to another file in your project:

```swift
struct ThanksView: View {
    var body: some View {
        Text("Thanks for checking out the app!")
    }
}
```

Then, wrap the "Hello" `Text` view into a `NavigationLink` view, where we set the destination parameter to `ThanksView`:

```swift
NavigationView {
    NavigationLink(destination: ThanksView()) {
        Text("Hello")
            .navigationTitle("Welcome")
    }
}
```

Now a lot of things are happening automatic: the `Hello` text turns blue and tappable:

![iPhone simulator showing Hello text as a blue tappable NavigationLink under Welcome title](https://flaviocopes.com/images/swiftui-navigationview/Screen_Shot_2021-09-23_at_18.15.27.png)

And once we tap on it, we're shown the `ThanksView` and a link to get back to the original view. The text shown in the top left button comes from the `navigationTitle` modifier we set:

![ThanksView displayed with back button and Thanks for checking out the app message](https://flaviocopes.com/images/swiftui-navigationview/Screen_Shot_2021-09-23_at_18.15.35.png)

Here's the full code used in the example:

```swift
import SwiftUI

struct ThanksView: View {
    var body: some View {
        Text("Thanks for checking out the app!")
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: ThanksView()) {
                Text("Hello")
                    .navigationTitle("Welcome")
            }
        }
    }
}
```

That's one way to navigate between views, and I'd say the simplest one.

Sometimes before navigating to another view you want to perform some action. In this case, we can have a boolean property `showThanks` that we can set to `true` when we want the `ThanksView` to appear. We do so when the user taps a button:

```swift
struct ContentView: View {
    @State private var showThanks = false

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: ThanksView(), isActive: $showThanks) {}

                Button("Hello") {
                    showThanks = true
                }
                .navigationTitle("Welcome")
            }
        }
    }
}
```

The app looks exactly the same as before, but now when the user taps the button, we can do something, like logging the transition or anything else:

```swift
Button("Hello") {
    showThanks = true
    print("Transitioned to ThanksView")
}
```

> Remember that `print()` does not log in preview mode, only in the Simulator
