helloimbrando
helloimbrando

Reputation: 655

SwiftUI Navigation Link Picker Style selection problem

I am working on a small todo app project with SwiftUI/Swift in Xcode 15.4. I have a model like this:

@Model
class Category {

 var catTitle: String = ""
 var catEmoji: String = ""

 var items:[toDoItem]?

 init(catTitle: String = "", catEmoji: String = ""){
    self.catTitle = catTitle
    self.catEmoji = catEmoji
 }
}

I have created two identical screens for users to create and update a to-do list. The code for both screens is the same. Both screens include a navigation stack and a list with a section that allows the user to select a category from a list using a picker with a navigation link style. Here is the code:

@Query private var categories: [Category]
@State var itemCategory: Category?

NavigationStack{
  List {
    ....
                //Category
                Section{
                    Picker("\(Image(systemName: "rectangle.stack"))  Category", selection: $itemCategory){
                        Text("No selection")
                            .tag(nil as Category?)
                        
                        ForEach(categories, id: \.self){ category in
                            Text("\(category.catEmoji)  \(category.catTitle)")
                                .tag(category as Category?)
                        }
                    }
                    .foregroundStyle(Color("my_accent"))
                    .frame(maxWidth:.infinity)
                    .labelsHidden()
                    .pickerStyle(.navigationLink)
                    .padding()
                    .overlay{
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(Color(.systemGray5), lineWidth: 2)
                            .fill(.clear)
                    }
                }
                .padding(.top)
                .listSectionSeparator(.hidden)
                .listRowBackground(Color("my_accent_inverted"))
    ....
  }
}

Now, when creating a todo everything works fine - the user can select a category and save it without any problems. However, when trying to update the todo selecting a new category from the picker, the selection isn't working.

This issue seems to only occur with the navigation link style picker. Other styles like ".menu" and ".wheel" work perfectly fine, allowing the user to update the todo without any issues.

Is there something else that needs to be done to make the picker work with a navigation link style? Could this issue be related to the update view being triggered through a navigation link?

Appreciate any help with this. Thank you.

Upvotes: 0

Views: 204

Answers (0)

Related Questions