Ios 用“传递错误”;执行segue“;返回零?
我试图向应用程序用户显示错误,当他们以错误的方式登录或注册时,例如,密码需要6个字符,因此如果他/她输入4或5,应该会给他带来错误,但它只在Xcode中打印,并且在应用程序中始终为零(我是新来的,网站告诉我图片大小太大,无法上载,因此我会写下来)。有人能帮我找出这里的代码有什么问题吗?谢谢Ios 用“传递错误”;执行segue“;返回零?,ios,swift,xcode,error-handling,segue,Ios,Swift,Xcode,Error Handling,Segue,我试图向应用程序用户显示错误,当他们以错误的方式登录或注册时,例如,密码需要6个字符,因此如果他/她输入4或5,应该会给他带来错误,但它只在Xcode中打印,并且在应用程序中始终为零(我是新来的,网站告诉我图片大小太大,无法上载,因此我会写下来)。有人能帮我找出这里的代码有什么问题吗?谢谢 // This is the code for the first viewcontroller where I try to pass the error to the ErrorViewControlle
// This is the code for the first viewcontroller where I try to pass the error to the ErrorViewController
if let email = emailTextfield.text, let password = passwordTextfield.text{
Auth.auth().signIn(withEmail: email, password: password) { [weak self] authResult, error in
if let e = error {
print(e)
self!.performSegue(withIdentifier: "GoToError", sender: self)
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "GoToError" {
let GoTo = segue.destination as! ErrorViewController
GoTo.errorText = "\(e.localizedDescription)"}
}
}else{
self!.performSegue(withIdentifier:K.loginSegue, sender: self)
}
}
}
// This is the code of the ErrorViewController where It should catch the error and pass it to the ErrorText label and get printed to the app screen
errorText: String?
@IBOutlet weak var ViewMessage: UIView!
@IBOutlet weak var ErrorText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
ViewMessage.layer.cornerRadius = ViewMessage.frame.size.height / 4
if errorText != nil {
ErrorText.text = errorText
}else{
// The string below is what gets printed because the error is always nil
ErrorText.text = "make sure your password is at least 6 characters"
原因是
prepare(对于segue
)必须在类的顶级实现(与viewDidLoad
的级别相同)
请以小写字母开头命名变量、函数和枚举实例。原因是
prepare(for segue
必须在类的顶层实现(与viewdiload
相同的级别)
func prepare(for segue: UIStoryboardSegue, sender: Any?)
请以小写字母开头命名变量、函数和枚举案例
func prepare(for segue: UIStoryboardSegue, sender: Any?)
每次segue发生时(当performsgue(withindicator:sender)函数调用时)都会调用此函数
viewController无法访问prepare()函数,因为它位于if/else块的本地范围内
更具体地说,prepare(for segue)函数只存在于if/else块的范围内,该块被包装在一个闭包中,该闭包被包装在另一个if/else块中
但是viewController应该始终可以访问此函数,这就是为什么它应该放在顶级作用域中的原因
那样
class MyViewController: UIViewController {
//other class properties and methods
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//your code here
}
}
我建议在swift中阅读更多关于范围和上下文的内容
每次segue发生时(当performsgue(withindicator:sender)函数调用时)都会调用此函数
viewController无法访问prepare()函数,因为它位于if/else块的本地范围内
更具体地说,prepare(for segue)函数只存在于if/else块的范围内,该块被包装在一个闭包中,该闭包被包装在另一个if/else块中
但是viewController应该始终可以访问此函数,这就是为什么它应该放在顶级作用域中的原因
那样
class MyViewController: UIViewController {
//other class properties and methods
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//your code here
}
}
我建议在swift中阅读更多关于范围和上下文的内容