使用 Binding<Int>使用 TextField SwiftUI [英] Use Binding&lt;Int&gt; with a TextField SwiftUI

查看:32
本文介绍了使用 Binding<Int>使用 TextField SwiftUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个页面以将玩家信息添加到本地数据库.对于每个链接到播放器结构中元素的输入,我都有一个 TextField 集合.

I am currently building a page to add player information to a local database. I have a collection of TextFields for each input which is linked to elements in a player struct.

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()
        }
    }

我的播放器结构是

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
}

问题是 ShirtNumber、Height 和 Weight 都是 Int 值.当我将它们绑定到 TextField 时,我收到一条错误消息,说 Cannot convert value of type 'Binding'到预期的参数类型 'Binding'.我所研究的关于 SwiftUI 的所有内容都表明,不可能有一个绑定了 Int 值的 TextField.

The issue is that ShirtNumber, Height, and Weight are all Int values. When I bind them to the TextField I get an error saying Cannot convert value of type 'Binding<Int>' to expected argument type 'Binding<String>'. Everything I have looked into about SwiftUI says it's impossible to have a TextField with an Int value bound to it.

我的问题是,是否可以创建一个新类来扩展 TextField 但只允许 Int 输入并绑定一个 Int 变量,像这样?

My question is, would it be possible to create a new class that extends TextField but allows for only Int inputs and that binds an Int variable, something like this?

struct IntTextField: TextField {

    init(_ text: String, binding: Binding<Int>) {

    }
}

到目前为止,我只能找到来自 this 问题.我正在寻找一种方法将其与 Binding 结合起来.

So far all I have been able to find is an answer to part of my question (accepting only Int input) from this question. I am looking for a way to combine this with the Binding<Int>.

感谢您的帮助.

推荐答案

其实可以用 TextField 绑定 manyTypes :

Actually , you can binding manyTypes with TextField:

      @State var weight: Int = 0
      var body: some View {


       Group{
       Text("\(weight)")
       TextField("Weight", value: $weight, formatter: NumberFormatter())
    }}

这篇关于使用 Binding<Int>使用 TextField SwiftUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆