选择器选择更改 SwiftUI 中的 TextField
我希望用户能够选择他们想要放入我的应用程序中的货币。但使用此代码:
import SwiftUI
struct ContentView: View {
@State private var amount = 0.0
@State private var currency = "USD"
let currencies = ["PLN", "USD", "EUR", "GBP", "JPY"]
var body: some View {
Form {
Picker("Currency", selection: $currency) {
ForEach(currencies, id: \.self) {
Text($0)
}
}
TextField("Amount", value: $amount, format: .currency(code: currency))
.keyboardType(.decimalPad)
}
}
}
使用 Picker 进行的货币选择不会更改 TextField 中的货币类型(它始终保持美元,即使在删除后也是如此)。如何将所选的货币代码推送到文本字段?
I want the user to be able to choose the currency they want to put in my app. But with this code:
import SwiftUI
struct ContentView: View {
@State private var amount = 0.0
@State private var currency = "USD"
let currencies = ["PLN", "USD", "EUR", "GBP", "JPY"]
var body: some View {
Form {
Picker("Currency", selection: $currency) {
ForEach(currencies, id: \.self) {
Text($0)
}
}
TextField("Amount", value: $amount, format: .currency(code: currency))
.keyboardType(.decimalPad)
}
}
}
The currency choices made with Picker don't change the currency type in the TextField (it stays USD at all times, even after deletion). How to push the chosen currency code to the TextField?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不会跟踪格式来更新 TextField,您可以根据格式参数使用 id 强制执行此操作,例如
使用 Xcode 13.2 / iOS 15.2 进行测试
Format is not tracked to update TextField, you can do it forcefully using
id
depending on format parameter, likeTested with Xcode 13.2 / iOS 15.2