简体   繁体   中英

Passing error with “perform segue” returns nil?

I am trying to show app users the error when they login in or register in a wrong way, for example the password needs 6 characters so if he/she entered 4 or 5 its should bring him error but it only prints in Xcode and always nil in the app (I am new here and the website tells me the picture size is too big to be uploaded so ill write down). Can someone help me figure what's wrong with the code here? Thanks!!

// 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"

The reason is that prepare(for segue must be implemented on the top level of the class (same level as viewDidLoad ).

And please name variables, functions and enum cases with starting lowercase letter.

func prepare(for segue: UIStoryboardSegue, sender: Any?)

This function is called every time segue occurs (when performSegue(withIndentifier:sender) function calls).

viewController has no access to prepare() function because it is in local scope of if/else block.

More specifically, prepare(for segue) function exists only in scope of if/else block, that is wrapped in a closure, that is wrapped in another if/else block.

But this function should be always accessible for viewController, that why it should be placed in a top level scope.

Like that

class MyViewController: UIViewController {

    //other class properties and methods

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //your code here
    }
}

I would suggest to read more about scope & context in swift.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM