Just starting off with swift/coding on xCode, at what point can I use a segue to take me to my homeVC?

Viewed 35

Was following a tutorial when I came to a halt at not being able to switch to a new VC.

The segue in question: self.performSegue(withIdentifier: "goHomeVC", sender: self)

I was trying to use it in the @IBAction didTapButton function, but it won't work and I can't figure out where else I would put it. After that button is tapped, when the user is logged in, everything on screen hides, but I want to have it go to homeVC.

import UIKit
import FirebaseAuth

class LoginVC: UIViewController, UITextFieldDelegate {


    private let companyLogo = UIImageView(image: UIImage(named: "Company Logo White.png")!);
    private let signInLogo = UIImageView(image: UIImage(named: "SignInText.png")!);


    private let emailField: UITextField = {
        let emailField = UITextField()
        emailField.placeholder = "Email Address"
        emailField.layer.borderWidth = 1
        emailField.autocapitalizationType = .none
        emailField.layer.borderColor = UIColor.white.cgColor
        emailField.backgroundColor = .white
        emailField.leftViewMode = .always
        emailField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 0))
        return emailField
    }()

    private let passwordField: UITextField = {
        let passwordField = UITextField()
        passwordField.placeholder = "Password"
        passwordField.layer.borderWidth = 1
        passwordField.isSecureTextEntry = true
        passwordField.layer.borderColor = UIColor.white.cgColor
        passwordField.backgroundColor = .white
        passwordField.leftViewMode = .always
        passwordField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 0))
        return passwordField
    }()

    @IBOutlet weak var button: UIButton!


    private let signOutButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = .systemBlue
        button.setTitleColor(.white, for: .normal)
        button.setTitle("Log Out", for: .normal)

        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()


        emailField.delegate = self
        passwordField.delegate = self


        NotificationCenter.default.addObserver(self, selector:
            #selector(keyboardWillChange), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector:
            #selector(keyboardWillChange), name: UIResponder.keyboardWillHideNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector:
            #selector(keyboardWillChange), name:UIResponder.keyboardWillChangeFrameNotification, object: nil)



        view.addSubview(companyLogo)
        view.addSubview(signInLogo)
        view.addSubview(emailField)
        view.addSubview(passwordField)
        view.addSubview(button)
        button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)

        if FirebaseAuth.Auth.auth().currentUser != nil {


            companyLogo.isHidden = true
            signInLogo.isHidden = true
            emailField.isHidden = true
            passwordField.isHidden = true
            button.isHidden = true

            view.addSubview(signOutButton)
            signOutButton.frame = CGRect(x: 20, y: 150, width: view.frame.size.width-40, height: 52)
            signOutButton.addTarget(self, action: #selector(logOutTapped), for: .touchUpInside)
        }
    }

    @objc private func logOutTapped(){
        do {
            try FirebaseAuth.Auth.auth().signOut()

            companyLogo.isHidden = false
            signInLogo.isHidden = false
            emailField.isHidden = false
            passwordField.isHidden = false
            button.isHidden = false

            signOutButton.removeFromSuperview()
        }
        catch {
            print("An error occurred")
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()



        companyLogo.frame = CGRect(x: 0,
                                y: 125,
                                width: 179,
                                height: 141)


        companyLogo.center.x = self.view.center.x // for horizontal
        //myButton.center.y = self.view.center.y // for vertical


        signInLogo.frame = CGRect(x: 0,
                            y: companyLogo.frame.origin.y+signInLogo.frame.size.height+150,
                            width: 110,
                            height: 36)
        signInLogo.center.x = self.view.center.x



        emailField.frame = CGRect(x: 20,
                                y: signInLogo.frame.origin.y+signInLogo.frame.size.height+50,
                                width: view.frame.size.width-40,
                                height: 50)
        emailField.addLayerEffects(cornerRadius: 5)



        passwordField.frame = CGRect(x: 20,
                                    y: emailField.frame.origin.y+emailField.frame.size.height+10,
                                    width: view.frame.size.width-40,
                                    height: 50)
        passwordField.addLayerEffects(cornerRadius: 5)



        button.frame = CGRect(x: 20,
                            y: passwordField.frame.origin.y+passwordField.frame.size.height+30,
                            width: view.frame.size.width-40,
                            height: 52)
        button.addLayerEffects(cornerRadius: 4)

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if FirebaseAuth.Auth.auth().currentUser == nil {
            //emailField.becomeFirstResponder()
        }
    }



    @IBAction func didTapButton(_ sender: Any) {
        print("Continue button tapped")
        guard let email = emailField.text, !email.isEmpty,
            let password = passwordField.text, !password.isEmpty else {
                print("missing field data")
                //self.performSegue(withIdentifier: "goHomeVC", sender: self)
                return
    }




        FirebaseAuth.Auth.auth().signIn(withEmail: email, password: password, completion: { [weak self] result, error in
            guard let strongSelf = self else {
                return
            }

            guard error == nil else {
                // show account creation
                strongSelf.showCreateAccount(email: email, password: password)
                return
            }

            print("You have signed in")

            strongSelf.signInLogo.isHidden = true
            strongSelf.emailField.isHidden = true
            strongSelf.passwordField.isHidden = true
            strongSelf.button.isHidden = true

            strongSelf.emailField.resignFirstResponder()
            strongSelf.passwordField.resignFirstResponder()

        })

    }

    func showCreateAccount(email: String, password: String) {
        let alert = UIAlertController(title: "Create Account",
                                    message: "Would you like to create an account?",
                                    preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Continue",
                        style: .default,
                        handler: {_ in

                            FirebaseAuth.Auth.auth().createUser(withEmail: email, password: password, completion: { [weak self] result, error in

                                guard let strongSelf = self else {
                                    return
                                }

                                guard error == nil else {
                                    // show account creation
                                    print("Account creation failed")
                                    return
                                }

                                print("You have signed in")

                                strongSelf.signInLogo.isHidden = true
                                strongSelf.emailField.isHidden = true
                                strongSelf.passwordField.isHidden = true
                                strongSelf.button.isHidden = true

                                strongSelf.emailField.resignFirstResponder()
                                strongSelf.passwordField.resignFirstResponder()

                            })

        }))
        alert.addAction(UIAlertAction(title: "Cancel",
                        style: .cancel,
                        handler: {_ in

        }))

        present(alert, animated: true)
    }



    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        emailField.resignFirstResponder()
        passwordField.resignFirstResponder()
        return true
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }


    @objc func keyboardWillChange(notification: Notification) {




        guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
                else {
                    return
                }

        if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {


            view.frame.origin.y = -245
        } else {

            view.frame.origin.y = 0
        }

    }





}
1 Answers

Hello, just to be sure. have you set the segue identifier in your storyboard? For navigation to work on calling performSegue(), you have to select a segue in your storyboard, then go to the attributes inspector and give it a name such as “goHomeVC” in your case.

Then in your swift code, to the place where you want to trigger the segue you just named, you need to call the method performSegue(); pass it a segue identifier as well as whatever object you want to send along, and you’re done.

Related