UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

NavigationLink destination - Set view programmatically

Forums > SwiftUI

Hi,

I am programming a treasure hunt for my daughter.

The main view consists of several NavigationLinks as image. If you click on a task (=image), the view of the individual task comes. Each tasks view is different.

HStack(alignment: .top, spacing: 20) {

    NavigationLink(
            destination: Task01View()
    ) {
     Text("1. Aufgabe")
       .font(.custom("Chalkduster", fixedSize: 16))
       .foregroundColor(Color(UIColor.brown))

        // ...  
    }

    NavigationLink(
            destination: Task02View()
    ) {
     Text("2. Aufgabe")
       .font(.custom("Chalkduster", fixedSize: 16))
       .foregroundColor(Color(UIColor.brown))

        // ...  
    }

    NavigationLink(
            destination: Task03View()
    ) {
     Text("3. Aufgabe")
       .font(.custom("Chalkduster", fixedSize: 16))
       .foregroundColor(Color(UIColor.brown))

        // ...  
    }

    // More NavigationLinks
}

The app has 24 tasks. There is a separate view for each task.

Currently I am hard-coding the navigation links into the source code.

However, my goal is to use a For loop to generate the images with the NavigationLinks. But my problem is the destination parameter in the NavigationLink.

How can I set the required view (e.g. Task01View or Task04View) automatically?

My idea.

for task in (1...6) {
        NavigationLink(
                                destination: ??? // Here is my problem
                ) {
                    Text("\(task). Aufgabe")
                        .font(.custom("Chalkduster", fixedSize: 16))
                        .foregroundColor(Color(UIColor.brown))
}

I also posted the problem on the Apple forum. Here is the link : https://developer.apple.com/forums/thread/691955

Thanks in advance for the support.

4      

With structs you have to go with a factory method which creates the view depending on a parameter. In the factory method you could use a switch statement to decide which view to show. Unlike classes you can't create structs from a string (yet). Perhaps in a future release of Swift.

3      

Hi, many thanks. Sorry for the late answer.

I have used the solution from the apple forum. This is perfect for me.

4      

@onqun  

This is cute :)

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.