4

I can't seem to find any information on this, anywhere. So I'm learning SwiftUI, and I'm trying to add a Picker to a Form, but the picker won't display the available options when tapped. I don't really know what else to tell you guys so could someone take a look and help me fix this? thanks :)

//Outside the body code
    @State var contacts: [String] = [""]
    @State var types: [Type] = [.mobile]
//Inside the body code and inside a Form{}
    Section(header: Text("Contact")){
            ForEach(0..<types.count, id: \.self) { i in
                HStack{
                    Picker(selection: $types[i], label: Text(""), content: {
                        Text("Mobile").tag(Type.mobile)
                        Text("Land Line").tag(Type.landline)
                        Text("Email").tag(Type.email)
                    }).pickerStyle(DefaultPickerStyle())
                    .frame(width: 80.0)
                    TextField("Insert Detail", text: $contacts[i])
                }
            }
            Button("Add Another") {
                types.append(.mobile)
                contacts.append("")
            }
5
  • ForEach(_:content:) should only be used for constant data. Instead conform data to Identifiable or use ForEach(_:id:content:) and provide an explicit id!. This is the message printed on console when I tested your code, and it’s clear what went wrong. Commented May 27, 2021 at 20:27
  • @TusharSharma what should the explicit Id look like? Commented May 27, 2021 at 20:49
  • ForEach(0..<types.count, id:\.self) and make sure each enum case in unique in types array. Commented May 27, 2021 at 20:53
  • correction, I remember this now, I had this, and changed to what I have n my question because I needed the index of the loop. Is there a way to get the index i while specifying the ID explicitly? Commented May 27, 2021 at 20:53
  • I suspect I now need to ask a new question to work out how to do what I need, If you wanna post your answer I'll accept it 4 ya :) @TusharSharma Commented May 27, 2021 at 20:58

2 Answers 2

4

I recently managed to resolve this problem.

Picker needs to be wrapped in a NavigationView before it will work in a form.

I was able to simply add a NavigationView to the top of my view and put the form inside it, and now the picker works perfectly.

var body: some View{
    NavigationView{
        Form{
            ....
            Picker()
            ....    
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned in comments-:

ForEach(:content:) should only be used for constant data. Instead conform data to Identifiable or use ForEach(:id:content:) and provide an explicit id!.

If you want to update your types array, change forEach line to ForEach(0..<types.count, id:\.self), and make sure your enum values are unique in array.

6 Comments

sorry, I just realised this didn't actually fix the problem -_- it got my button working, but not my Picker (which was the main point of the question lol)
what isn’t working in picker? It’s working fine for me.
it will show me "Mobile", the rawValue for the .mobile already in the types array, but when I tap on the picker to change the selection nothing happens
Nothing in the log for me either, Not even sure how to debug this
seems like a bug with forms in iOS 14.5
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.