The issue you're facing where the app crashes after adding more than 16 NavigationLinks to a NavigationStack is likely related to a SwiftUI limitation known as the "10-View Limit." SwiftUI imposes a limit on the number of NavigationLink views that can be placed in a single navigation stack, which is usually around 10 to 16, depending on the device and iOS version.
To work around this limitation, you can consider organizing your medication list differently. Instead of creating a separate view for each medication and using NavigationLinks, you can create a dynamic list of medications using a ForEach loop and then pass the selected medication to a detail view when tapped. Here's an example of how you can do that:
struct Medication: Identifiable {
let id = UUID()
let name: String
// Add any other properties you need for each medication
}
struct Medikamente: View {
let medications: [Medication] = [
Medication(name: "Acetylsalicylsäure"),
Medication(name: "Amiodaron"),
Medication(name: "Atropin"),
// Add more medications here
]
@State private var selectedMedication: Medication? = nil
var body: some View {
NavigationView {
List(medications) { medication in
Button(action: {
selectedMedication = medication
}) {
Text(medication.name)
}
}
.sheet(item: $selectedMedication) { medication in
MedicationDetailView(medication: medication)
}
.navigationTitle("Medikamente")
}
}
}
struct MedicationDetailView: View {
let medication: Medication
var body: some View {
// Create a detailed view for the selected medication
Text("Details for \(medication.name)")
// Add more content as needed
}
}
In this example, we create a list of medications using a ForEach loop and use a @State variable to track the selected medication. When a medication is tapped, we display a detail view for that medication using the .sheet modifier.
This approach should allow you to display a list of medications without hitting the limitation imposed by SwiftUI's NavigationLink behavior.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: