SwiftUI-无法在表单内的选择器视图中添加修改器

时间:2020-10-31 10:02:25

标签: swiftui picker pickerview

我目前正在从事#hackingwithswift的Project 4。我的问题是无法从表单视图中更改选择器的样式。

这是我正在处理的代码的一小段。请假定其余代码正确。无论我在.pickerStyle()中加上什么修饰符,我总是会得到错误:

类型'WheelPickerStyle.Type'不符合'PickerStyle';只要 struct / enum / class类型可以符合协议

NavigationView{
        Form{
            Section(header: Text("When do you want to wake up?") .font(.headline)) {
                
                DatePicker("Please enter a time", selection: $wakeUp, displayedComponents: .hourAndMinute)
                    .datePickerStyle(WheelDatePickerStyle())
                    .labelsHidden()
            }
            
            Section(header: Text("Desired amount of sleep")
                .font(.headline)) {
                
                Stepper(value: $sleepAmount, in: 4...12, step: 0.25) {
                    Text("\(sleepAmount, specifier: "%g") hours")
                }
            }
            
            Section(header: Text("Daily coffee intake")
                .font(.headline)) {
                    
                    Picker("Number of coffees had in a day", selection: $coffeeAmount) {
                            Text("1")
                            Text("2")
                            Text("3")
                        }
                    .pickerStyle(WheelPickerStyle)
                    }

有人可以帮我吗?请说明我在做什么错,我是初学者!

谢谢!

1 个答案:

答案 0 :(得分:1)

在您的代码中

Picker("Number of coffees had in a day", selection: $coffeeAmount) {
                            Text("1")
                            Text("2")
                            Text("3")
                        }
                    .pickerStyle(WheelPickerStyle)
                    }

您应该使用WheelPickerStyle()而不是WheelPickerStyle