【发布时间】:2023-01-11 17:52:15
【问题描述】:
var body: some View {
VStack(alignment: .center) {
func CheckInput() { //<-- here the error
if(name == "") {
self.showAlertC1 = true
}
else if(!checkInput(name)) {
self.showAlertC2 = true
}
else {
NavigationLink(destination: DisplayView(name: name)){
Text("Tap Me")
}
)
}
}
Text("enter you name")
TextField("Enter your name...", text: $name, onEditingChanged: { [weak self] (editing) in
if !editing {
self?.checkInput()
}
})
}
.alert(isPresented: $showAlertC1) {
Alert(title: Text("Error"), message: Text("Input is empty"), dismissButton: .default(Text("OK")))
}
.alert(isPresented: $showAlertC2) {
Alert(title: Text("Error"), message: Text("Input is not valid"), dismissButton: .default(Text("OK")))
}
}
所以我得到了错误(包含声明的闭包不能与结果生成器“ViewBuilder”一起使用)
基本上,如果用户在完成键入或单击“输入键盘”后,它应该转到 func 并检查输入,如果有任何错误应该弹出警报解释问题
@State private var name: String = ""
@State private var showAlertC1 = false
@State private var showAlertC2 = false
func checkInput(_ input: String) -> Bool { let alphabet = CharacterSet.letters return input.rangeOfCharacter(from: alphabet) != nil }
【问题讨论】:
-
为什么
func CheckInput() { ... }在VStack(...) { ... here ... }里面?它应该与var body: some View { ... }处于同一“级别”吗?
标签: swift