这是我的代码...
struct ContentView : View {
@State var showingTextField = false
@State var text = ""
var body: some View {
return VStack {
if showingTextField {
TextField($text)
}
Button(action: {
self.showingTextField.toggle()
}) {
Text ("Show")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的是当文本字段变为可见时,使文本字段成为第一响应者(即,获得焦点并弹出键盘)。
我正在用钱输入屏幕,需要实现一个自定义项,init以根据初始化的金额设置状态变量。
我以为这可以用,但是出现编译器错误:
Cannot assign value of type 'Binding<Double>' to type 'Double'
struct AmountView : View {
@Binding var amount: Double
@State var includeDecimal = false
init(amount: Binding<Double>) {
self.amount = amount
self.includeDecimal = round(amount)-amount > 0
}
...
}
Run Code Online (Sandbox Code Playgroud) 升级到Xcode 11 Beta 4之后,在String(format: , args)与@Stateproperty一起使用时,我开始看到错误。请参见下面的代码。第二Text行抛出一个错误:
表达式类型“字符串”不明确,没有更多上下文
而Texts 1、3和4可以正常工作。
struct ContentView : View {
@State var selection = 2
var body: some View {
VStack {
Text("My selection \(selection)") // works
Text("My selection \(String(format: "%02d", selection))") // error
Text("My selection \(String(format: "%02d", Int(selection)))") // works
Text("My selection \(String(format: "%02d", $selection.binding.value))") // works
}
}
}
Run Code Online (Sandbox Code Playgroud)
我意识到这是Beta版软件,但很好奇是否有人可以看到此行为的原因,或者仅仅是一个错误。如果无法解释,我将提交雷达报告。