Следил за инструкцией, когда я остановился из-за невозможности переключиться на новый V C.
Рассматриваемый переход: self.performSegue(withIdentifier: "goHomeVC", sender: self)
Я пытался использовать его в функции @IBAction didTapButton
, но это не сработает, и я не могу понять, где еще я бы его поместил. После нажатия этой кнопки, когда пользователь вошел в систему, все на экране скрывается, но я хочу, чтобы go переходил в homeV C.
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
}
}
}