1

I have a NavigationView showing a title with the number of records in a List:

.navigationTitle("\(navBarTitle) (\(String(numberOfRecords)))")

In the toolbar I have 2 buttons:

  • show Unread - only show unread / show all
  • show Favorites - only show favorites / show all

The List view shows either All records, Unread records or Favorite records. Switching between Views using the buttons works fine and updates the navigation title numberOfRecords variable.

All is well sofar. Now...

Rows in the list have a NavigationLink that goes to a detail view:

NavigationLink(destination: detailView(record: record))

In the detail view I have a button: make card favorite. I click the button, but when returning to the list view, the numberOfRecords variable in the navigation title is not updated.

How can I update the navigation title from its detail view?

Thank you!

Ken White
  • 123,280
  • 14
  • 225
  • 444
JoostS
  • 21
  • 3
  • 1
    Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and see: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). The answer is yes, but it depends upon your code as to how to do it. – Yrb Nov 18 '21 at 20:09
  • 1
    This is a great example for the use of a Published variable and observable object. Make the nav bar reflect the value of this observed publish value, then when you update the variable from another view, it is updated everywhere. It is like a State variable but potentially app wide.. https://www.hackingwithswift.com/quick-start/swiftui/observable-objects-environment-objects-and-published – JerseyDevel Nov 23 '21 at 02:22
  • 1
    Environment objects may be better suited for your application: https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views – JerseyDevel Nov 23 '21 at 02:24

0 Answers0