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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.