Ber*_*ard 27 xcode swift swiftui
我目前正在构建一个页面以将玩家信息添加到本地数据库。我为每个链接到播放器结构中的元素的输入都有一个 TextField 集合。
var body: some View {
VStack {
TextField("First Name", text: $player.FirstName)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("Last Name", text: $player.LastName)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("Email", text: $player.eMail)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("Shirt Number", text: $player.ShirtNumber)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("NickName", text: $player.NickName)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("Height", text: $player.Height)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("Weight", text: $player.Weight)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: {
submitPlayer(player: self.player)T
}) {
Text("Submit")
}
Spacer()
}
}
Run Code Online (Sandbox Code Playgroud)
我的播放器结构是
struct Player: Hashable, Codable, Identifiable {
var id: Int
var FirstName: String
var LastName: String
var NickName: String
var eMail: String
var ShirtNumber: Int
var Height: Int
var Weight: Int
}
Run Code Online (Sandbox Code Playgroud)
问题是 ShirtNumber、Height 和 Weight 都是 Int 值。当我将它们绑定到 TextField 时,我收到一条错误消息Cannot convert value of type 'Binding<Int>' to expected argument type 'Binding<String>'。我所研究的关于 SwiftUI 的所有内容都表明,不可能有一个带有 Int 值的 TextField 绑定到它。
我的问题是,是否可以创建一个新类来扩展 TextField 但只允许 Int 输入并绑定一个 Int 变量,像这样?
struct IntTextField: TextField {
init(_ text: String, binding: Binding<Int>) {
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,所有我已经能够找到的是从回答我的问题的一部分(只接受INT输入)这个问题。我正在寻找一种将其与Binding<Int>.
谢谢您的帮助。
E.C*_*oms 58
实际上,您可以使用以下方法绑定 manyTypes TextField:
@State var weight: Int = 0
var body: some View {
Group{
Text("\(weight)")
TextField("Weight", value: $weight, formatter: NumberFormatter())
}}
Run Code Online (Sandbox Code Playgroud)
Asp*_*eri 28
当然也可以使用
TextField("", value: $value, formatter: NumberFormatter())
// .keyboardType(UIKeyboardType.decimalPad) // << uncomment for num pad
Run Code Online (Sandbox Code Playgroud)
甚至使用数字键盘,但这并不能阻止将非数字字符输入到此类中TextField,并且直到formatter未调用commit来验证输入。也许 Apple 会给我们在未来即时验证输入的可能性,但不是现在,......所以我更喜欢不同的方式
这是我为数值(Int、Float、Double 等)设置文本字段的方法,它验证特定类型的输入和限制(比如不允许输入比 Int 最大允许值更长的值)。希望它对某人也有帮助。(当然可以根据使用需要进行字体、大小、颜色等配置)
struct NumberTextField<V>: UIViewRepresentable where V: Numeric & LosslessStringConvertible {
@Binding var value: V
typealias UIViewType = UITextField
func makeUIView(context: UIViewRepresentableContext<NumberTextField>) -> UITextField {
let editField = UITextField()
editField.delegate = context.coordinator
return editField
}
func updateUIView(_ editField: UITextField, context: UIViewRepresentableContext<NumberTextField>) {
editField.text = String(value)
}
func makeCoordinator() -> NumberTextField.Coordinator {
Coordinator(value: $value)
}
class Coordinator: NSObject, UITextFieldDelegate {
var value: Binding<V>
init(value: Binding<V>) {
self.value = value
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
let text = textField.text as NSString?
let newValue = text?.replacingCharacters(in: range, with: string)
if let number = V(newValue ?? "0") {
self.value.wrappedValue = number
return true
} else {
if nil == newValue || newValue!.isEmpty {
self.value.wrappedValue = 0
}
return false
}
}
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
if reason == .committed {
textField.resignFirstResponder()
}
}
}
}
struct TestTextFieldWithNumbers: View {
@State private var value = 0
var body: some View {
VStack {
Text("Current value: \(value)")
Divider()
TextField("", value: $value, formatter: NumberFormatter())
// .keyboardType(UIKeyboardType.decimalPad)
Divider()
NumberTextField(value: $value)
.frame(height: 32)
}
}
}
struct TestTextFieldWithNumbers_Previews: PreviewProvider {
static var previews: some View {
TestTextFieldWithNumbers()
}
}
Run Code Online (Sandbox Code Playgroud)
只是为了让它更容易观察。
改变这个:
TextField("", text: $intvalue)
Run Code Online (Sandbox Code Playgroud)
到那个
TextField("Value", value: $amount, formatter: NumberFormatter())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17007 次 |
| 最近记录: |