Hi there,
I am currently programming an app for certain medication usage for paramedics in my region. For that, I made a List
inside a NavigationStack
with NavigationLinks
to further Swift files, each one for one drug. The problem is, that when I add more than 16 NavigationLinks
to the NavigationStack
, the app stops working and crashes. Is it too much code or do I have to do anything else? I am very new to programming in general so I would love to hear from you!
Greetings Laurin
Here is the code:
//
// Medikamente.swift
// SAA Medikamente
//
// Created by Laurin Bräunig on 18.10.23.
//
import SwiftUI
struct Medikamente: View {
var body: some View {
// Medikamentenliste
NavigationStack {
//Medikamente
List {
NavigationLink(destination: Acetylsalicylsaeure()){
Text("Acetylsalicylsäure")
}
NavigationLink(destination: Amiodaron()){
Text("Amiodaron")
}
NavigationLink(destination: Atropin()){
Text("Atropin")
}
NavigationLink(destination: Butylscopolamin()){
Text("Butylscopolamin")
}
NavigationLink(destination: Dimenhydrinat()){
Text("Dimenhydrinat")
}
NavigationLink(destination: Dimentinden()){
Text("Dimentinden")
}
NavigationLink(destination: Epinephrin()){
Text("Epinephrin")
}
NavigationLink(destination: Esketamin()){
Text("Esketamin")
}
NavigationLink(destination: Furosemid()){
Text("Furosemid")
}
NavigationLink(destination: Glucagon()){
Text("Glucagon")
}
NavigationLink(destination: Glucose()){
Text("Glucose")
}
NavigationLink(destination: Glyceroltrinitrat()){
Text("Glyceroltrinitrat")
}
NavigationLink(destination: Heparin()){
Text("Heparin")
}
NavigationLink(destination: Ibuprofen()){
Text("Ibuprofen")
}
NavigationLink(destination: Ipratropiumbromid()){
Text("Ipratropiumbromid")
}
NavigationLink(destination: Lidocain()){
Text("Lidocain")
}
.navigationTitle("Medikamente")
.navigationBarTitleDisplayMode(.automatic)
}
}
}
}
#Preview {
Medikamente()
}
Thanks again for your help!
If you have multiple NavigationStacks and detailed descriptions for medications, and you need to link to details from other pages, you can still organize your code to avoid SwiftUI's NavigationLink limitation. Here's a way to achieve this:
- Use a ViewModel: Create a ViewModel that holds information about each medication, including its name and detailed description.
struct MedicationViewModel: Identifiable {
let id = UUID()
let name: String
let detailedDescription: String
// Add any other properties you need for each medication
}
- Create a Centralized Medication Data Source: Have a central data source that contains all the medication information. This data source can be an ObservableObject that you can pass around to different views in your app.
class MedicationDataSource: ObservableObject {
@Published var medications: [MedicationViewModel] = []
init() {
// Initialize medications with data
medications = [
MedicationViewModel(name: "Acetylsalicylsäure", detailedDescription: "Details for Acetylsalicylsäure"),
MedicationViewModel(name: "Amiodaron", detailedDescription: "Details for Amiodaron"),
// Add more medications and descriptions here
]
}
}
- Use NavigationLink in a Central View: In your central view, such as your
Medikamente
view, use aList
or other UI element to display the medication names and create NavigationLinks to navigate to the detailed views.
struct Medikamente: View {
@ObservedObject var dataSource = MedicationDataSource()
var body: some View {
NavigationView {
List(dataSource.medications) { medication in
NavigationLink(destination: MedicationDetailView(medication: medication)) {
Text(medication.name)
}
}
.navigationTitle("Medikamente")
}
}
}
- Create Detailed Views: For each medication, create a detailed view (
MedicationDetailView
) that takes theMedicationViewModel
as a parameter and displays the detailed information.
struct MedicationDetailView: View {
let medication: MedicationViewModel
var body: some View {
VStack {
Text(medication.name)
Text(medication.detailedDescription)
// Add more content as needed
}
.navigationTitle("Details")
}
}
With this approach, you can easily add as many medications as you need without hitting SwiftUI's NavigationLink limitation. You can also navigate to detailed views from various pages in your app by sharing the MedicationDataSource
. This centralizes your data and makes it more maintainable as your app grows.