0

I have a problem regarding NagivationBarTitle.

I have a view (first view) from which I call another one (second view). In the second view I have some photos which I can select. If no Images are selected the NavigationBar title is "Images". If I have images selected I have the title like "x Images selected". If I have images selected I can press a button which will display an ActionSheet.

The problem is that when the action sheet is shown the title is going back to "Images". How can I preserve the title in ActionSheet as it was before tapping ("x Images selected")?

Some example of code when action sheet is shown:

First View:

struct FirstView: View {
    var body: some View {
            VStack (alignment: .leading) {
                List {
                    AnotherView(param: param)
                    .navigationBarItems(trailing:
                    NavigationLink(destination: SecondView()) {
                            Image(name: "search")
                        }
                    )
                }
            }
    }
}

Second View:

struct SecondView: View {
    @State private var imagesSelected: Int = 0
var body: some View {
    VStack {
        List {
                // here is a grid of items. every time an item is clicked a
                // counter in encreasing
                when item clicked: imagesSelected++
        }
        .navigationBarTitle("\(self.imagesSelected) Files selected")
        .actionSheet(isPresented: $showingActionSheet) {
            ActionSheet(title: Text("Delete Images"), buttons: [
                .destructive(Text("Delete")){
                    // Some action
                },
                .cancel()
            ])
        }
    }
}

}

  • Please show the code of your views as well. – pawello2222 Jul 24 '20 at 15:18
  • [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Asperi Jul 24 '20 at 15:57
  • Your code is still not testable. If I mock missing parts it works without problems. Please paste a code that actually *compiles*. – pawello2222 Jul 24 '20 at 20:11
  • 1
    Thank you for response. I've tried to simply the code is order to be more readable. When you said to post the entire code I've also discovered the problem :)). I've discovered the problem. I was updating the title in a wrong layer and was not visible to the whole view. I mean the action sheet was in another layer where the title of the NavigationBar was the old one. And that was the problem of showing the old title. – Theodor Ungureanu Jul 24 '20 at 21:42

0 Answers0