"perform segue" returns nil 传递错误?
Passing error with "perform segue" returns nil?
我试图在应用程序用户以错误的方式登录或注册时向他们显示错误,例如密码需要 6 个字符,所以如果 he/she 输入 4 或 5,应该会给他带来错误,但它仅在 Xcode 中打印并且在应用程序中始终为零(我是新来的,网站告诉我图片尺寸太大而无法上传,所以我记下来了)。有人可以帮我弄清楚这里的代码有什么问题吗?谢谢!!
// 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
必须在 class 的顶层实现(与 viewDidLoad
相同)。
并且请以小写字母开头的变量、函数和枚举案例命名。
func prepare(for segue: UIStoryboardSegue, sender: Any?)
每次发生 segue 时都会调用此函数(当 performSegue(withIndentifier: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 中有关范围和上下文的更多信息。
我试图在应用程序用户以错误的方式登录或注册时向他们显示错误,例如密码需要 6 个字符,所以如果 he/she 输入 4 或 5,应该会给他带来错误,但它仅在 Xcode 中打印并且在应用程序中始终为零(我是新来的,网站告诉我图片尺寸太大而无法上传,所以我记下来了)。有人可以帮我弄清楚这里的代码有什么问题吗?谢谢!!
// 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
必须在 class 的顶层实现(与 viewDidLoad
相同)。
并且请以小写字母开头的变量、函数和枚举案例命名。
func prepare(for segue: UIStoryboardSegue, sender: Any?)
每次发生 segue 时都会调用此函数(当 performSegue(withIndentifier: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 中有关范围和上下文的更多信息。