Swiftui Textfield State Change停止使用iOS 15.4.1
直到iOS 15.4.1,以下代码一直运行良好。在输入的值大于100000之后,它只能启用一个文本字段旁边的按钮。它仍然可以与15.3.1一起使用,但停止使用15.4.1。知道什么是为了防止其工作而发生的变化吗?
import SwiftUI
struct ContentView: View {
@State private var woid:Int? = nil
@State var showResults = false
private var woidFormatter:NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = ""
return formatter
}()
var body: some View {
Form {
Section(header: Text("Work Order Lookup")) {
HStack {
TextField("Work Order Number", value: $woid, formatter: woidFormatter)
.keyboardType(.numberPad)
Spacer()
Button("Lookup") {
showResults.toggle()
}.buttonStyle(PurpleButton(disabled: (woid ?? 0) < 100000))
.disabled((woid ?? 0) < 100000)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct PurpleButton: ButtonStyle {
let disabled:Bool
let cardColor:Color = Color(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1))
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.background(disabled ? Color.gray : cardColor)
.foregroundColor(.white)
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
}
}
The following code has worked fine until iOS 15.4.1. It simply enables a button next to a textfield after the value entered is greater than 100000. It still works with 15.3.1 but stopped working with 15.4.1. Any idea what was changed to prevent it from working?
import SwiftUI
struct ContentView: View {
@State private var woid:Int? = nil
@State var showResults = false
private var woidFormatter:NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = ""
return formatter
}()
var body: some View {
Form {
Section(header: Text("Work Order Lookup")) {
HStack {
TextField("Work Order Number", value: $woid, formatter: woidFormatter)
.keyboardType(.numberPad)
Spacer()
Button("Lookup") {
showResults.toggle()
}.buttonStyle(PurpleButton(disabled: (woid ?? 0) < 100000))
.disabled((woid ?? 0) < 100000)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct PurpleButton: ButtonStyle {
let disabled:Bool
let cardColor:Color = Color(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1))
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.background(disabled ? Color.gray : cardColor)
.foregroundColor(.white)
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么它以前有效,但是使用选项的适当签名是:
更改此行:
为此
I'm not sure why it worked before but the proper signature for using optionals is this one:
Changing this line:
to this works