Saturday, November 21, 2026
HomeiOS Developmentios - SwiftUI - Dynamic Picker from String Array not updating Array...

ios – SwiftUI – Dynamic Picker from String Array not updating Array Values in UI

[ad_1]

So I’ve three string arrays: one is all of the choices the person provides or removes (selectedOptions), and one is choices for the person to set from (availableOptions). When the person presses a + button on a stepper on the backside of my UI, it provides a brand new string to the selectedOptions array (a string merely set to “None”). The third is a availableOptions array displaying a string for every accessible possibility.

I then use a ForEach to replace the UI to indicate this selection, together with a picker to permit the person to vary the precise merchandise within the selectedOptions array to the choices of their selection. The Picker reveals all of the choices from the availableOptions array and, when one is chosen, its entry within the selectedOptions array is up to date, and that corresponding possibility is faraway from the availableOptions array (and vice versa if the person modifications to a different possibility, that possibility is faraway from chosen and put again into accessible) so that each one the choices apart from “None” can’t be chosen twice. To verify the order of the choices does not change, I’ve a operate that makes use of an array of all of the choices from the beginning and, when the selectedOptions array modifications, it takes the originalOptions array and removes any selectedOptions from it, and writes this to the availableOptions property.

The Drawback: once I run this code and add a brand new merchandise to the selectedOptions array, after which change the merchandise, it does not replace the array. Solely once I add a brand new merchandise to the array does it then present the values of all of the gadgets. It additionally takes the show identify of every merchandise and resets it when new gadgets are added or faraway from the array. The issue is primarily considered one of UI updating and UI usefulness because the code (for essentially the most half) does what it must to functionality-wise.

My purpose is for the picker to reference its particular merchandise within the array and, when the person modifications it on the picker, the picker choice modifications AND the merchandise within the array will get changed by the brand new “picked” merchandise. This fashion, when the UI masses, it will possibly take the “selectedOptions” array and populate the gadgets within the ForEach with the right choices.

I do not know if that is attainable, however in a Hacking with Swift tutorial it reveals a string property being up to date/modified primarily based on a picker, so I am making an attempt to do this however with a selected merchandise in an array.

Under is my code, hopefully what I am making an attempt to realize is smart to somebody:

@State var selectedOptions: [String] = [] {
        didSet {
            removeUsedOptionsFromAvailable()
        }
    }
    
    @State var availableOptions = ["None", "One", "Two", "Three", "Four", "Five"]
    
    let startingOptions = ["None", "One", "Two", "Three", "Four", "Five"]

    func removeUsedOptionsFromAvailable() {
        availableOptions = startingOptions
        for possibility in selectedOptions {
            if possibility != "None" {
                availableOptions = availableOptions.filter { $0 != possibility }
            }
            
        }
    }
    
    var physique: some View {

        return NavigationView {
            VStack {
                ScrollView {
                    VStack {
                        ForEach(selectedOptions.indices, id: .self) { index in
                                ZStack {
                                    HStack {
                                        Spacer()


                                        Textual content("Entry #(index + 1): ")
                                            
                                        
                                        Spacer()

                                        Picker("", choice: $selectedOptions[index]) {
                                            ForEach(availableOptions.indices, id: .self) {
                                                Textual content(availableOptions[$0])
                                                    .tag(availableOptions[$0])
                                            }

                                        }
                                        .pickerStyle(.menu)
                                        .id(self.selectedOptions)
                                        
                                        
                                        
                                        Picture(systemName: "minus.circle.fill")

                                            .onTapGesture {
                                                self.selectedOptions.take away(at: index)
                                                removeUsedOptionsFromAvailable()
                                            }
                                        
                                    }
                                    VStack {
                                        Spacer()
                                            .body(peak: Ok.screenHeight / 15)
                                        Divider()
                                    }
                                }
                                
                            }
                           
                            
                        
                    }
                }
                
                
                    
                if selectedOptions.isEmpty {
                        VStack {
                            Spacer()
                            Textual content("Faucet Plus Button to Add a Worth")
                                
                        }
                        
                    }
                    Spacer()
                    
                    HStack {
                        Spacer()
                          
                        
                        Textual content("Add/Take away Worth")
                                                    
                        
                        Spacer()
                        
                        
                        Stepper(onIncrement: {
                            
                            if self.selectedOptions.rely != self.startingOptions.rely {
                                selectedOptions.append(availableOptions[0])
                                removeUsedOptionsFromAvailable()
                            }
                            
                            
                            
                        },
                                
                                onDecrement: {
                            
                            
                            if !self.selectedOptions.isEmpty {
                                
                                self.selectedOptions.removeLast(1)
                                
                                
                                
                            }
                            
                        }) {
                            
                            Textual content("")
                        }
                        .labelsHidden()
                        .padding(.vertical, Ok.screenHeight / 70)
                        Spacer()
                            .body(width: Ok.screenWidth / 20)
                    }
                    
                }
                

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments