我有一个LoginController,它显示一个LoginView。当用户输入电子邮件和密码时,将出现一个名为CockpitController的名为cockpitController的新ViewController。我不使用情节提要。问题:当我按下登录按钮时,loginController似乎没有收到操作。@objc func loginButtonClicked()
应该在func login
中调用loginController
。我在这里想念的东西。
LoginController
import UIKit
import Firebase
class LoginController: UIViewController {
var loginView: LoginView { return self.view as! LoginView }
var homeController: HomeController?
override func viewDidLoad() {
super.viewDidLoad()
}
override func loadView() {
self.view = LoginView(frame: UIScreen.main.bounds)
loginView.setupViews()
}
var cockpitController: CockpitController?
func login(email: String, password: String) {
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
guard error == nil else {
// ...
return
}
self.present(self.cockpitController!, animated: true, completion: nil)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
}
LoginView]
import UIKit
class LoginView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.red
setupViews()
}
var loginController: LoginController?
let userEmailTextField: UITextField = {
let textField = UITextField()
textField.backgroundColor = UIColor.white
textField.borderStyle = .roundedRect
return textField
}()
let passwordTextField: UITextField = {
let textField = UITextField()
textField.backgroundColor = UIColor.white
textField.borderStyle = .roundedRect
return textField
}()
let loginButton: UIButton = {
let button = UIButton()
button.setTitle("Login", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.setTitleColor(UIColor.black, for: .highlighted)
button.backgroundColor = UIColor.red
button.addTarget(self, action:#selector(loginButtonClicked), for: .touchUpInside)
return button
}()
let signUpButton: UIButton = {
let button = UIButton()
button.setTitleColor(UIColor.black, for: .normal)
button.setTitle("Creat a Account", for: .normal)
return button
}()
@objc func loginButtonClicked() {
let email = userEmailTextField.text
let password = passwordTextField.text
self.loginController?.login(email: email!, password: password!)
}
func setupViews() {
//... addSubviews
//... constraints
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
您需要设置loginController
的loginView
。
override func loadView() {
self.view = LoginView(frame: UIScreen.main.bounds)
loginView.loginController = self
loginView.setupViews()
}
提示:此外,将loginController
中的loginView
修改为weak var
,以避免参考循环。