ios - 使用“执行segue”传递错误返回nil?
问题描述
我试图在应用程序用户以错误的方式登录或注册时向他们显示错误,例如密码需要 6 个字符,所以如果他/她输入 4 或 5,它应该会给他带来错误,但它只在 Xcode 中打印并且总是 nil在应用程序中(我是新来的,网站告诉我图片太大而无法上传,所以我记下来了)。有人可以帮我弄清楚这里的代码有什么问题吗?谢谢!!
// 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(for segue
必须在类的顶层(与 同级viewDidLoad
)上实现。
并且请以小写字母开头命名变量、函数和枚举案例。
推荐阅读
- javascript - 从滚动的大量数据中添加/删除记录的最佳方法
- c++ - tf2_ros::Buffer::canTransform() 为现有转换返回 False
- html - 使用引导程序更改 8 个 div 的顺序
- android - 究竟是什么导致 adb devices => “设备离线”
- python - 尽管验证集和训练集不同,但 TensorFlow 验证和训练准确度相同
- javascript - 折叠完成后如何获取 href 值?
- python - 如何将 Hive 中选定列号的浮点数重铸为整数?
- vb.net - 将 IEnumerable(Of DataRow()) 转换为 List(Of Datarow)
- javascript - Storing game state when game state includes functions
- python - 为什么不会破?