Post

Replies

Boosts

Views

Activity

How can I make a new item from within a navigationlink?
Sorry if this is too basic, but I really can't figure out how to make this work. I am trying to make a journal app and I want to have a button that creates a new journalentry and then loads it. This code snippet (I removed irrelevant bits) shows how I'm trying to go about it. Basically, the newEntryButtons should launch a JournalPromptRoute for one of the different options: that's the method argument. But putting .createNewEntry() inside the argument means that new entries are being constantly created. I want the system to create a new entry once on the button press and then go to it from the navigationDestination area. Can anyone please explain to me how this is supposed to work? I believe the code snippet is detailed enough to get the point across but please let me know if you need more info. case library case blank(entry: Entry) case mystery case summary(entry: Entry) case entry(entry: Entry) } struct JournalHome: View { @ObservedObject var viewModel: JournalViewModel var mainContent: some View { ScrollView { newEntryButtons LazyVStack { ForEach(filteredEntries) { entry in NavigationLink( value: JournalPromptRoute.entry(entry: entry) ) { JournalCard(entry) } } } } } var body: some View { NavigationStack(path: $path) { mainContent .navigationDestination(for: JournalPromptRoute.self) { route in switch route { case .blank(let entry): JournalEntryView( entry: entry, index: 0, viewModel: viewModel ) } } } } var newEntryButtons: some View { ScrollView(.horizontal) { HStack(spacing: 15) { EntryButton( description: "Add blank entry", method: JournalPromptRoute.blank(entry: viewModel.createNewEntry()), viewModel: viewModel, path: $path ) EntryButton( description: "Select a Journey", method: JournalPromptRoute.library, viewModel: viewModel, path: $path ) EntryButton( description: "Let the Journey find you", method: JournalPromptRoute.mystery, viewModel: viewModel, path: $path ) } } } }
Topic: UI Frameworks SubTopic: SwiftUI
2
0
61
May ’25