用swift写一个登陆界面
时间: 2023-12-29 20:45:56 浏览: 162
Swift 实现的登录界面
以下是一个简单的登陆界面示例代码:
```
import UIKit
class LoginViewController: UIViewController {
//MARK: - Outlets
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var loginButton: UIButton!
//MARK: - Lifecycle Methods
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
}
//MARK: - UI Configuration Methods
private func configureUI() {
loginButton.layer.cornerRadius = 5.0
}
//MARK: - Actions
@IBAction func loginButtonTapped(_ sender: UIButton) {
guard let email = emailTextField.text, !email.isEmpty else {
displayAlert(message: "Please enter your email.")
return
}
guard let password = passwordTextField.text, !password.isEmpty else {
displayAlert(message: "Please enter your password.")
return
}
//Perform login API call or other login logic here
}
//MARK: - Helper Methods
private func displayAlert(message: String) {
let alertController = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}
}
```
这个登陆界面包括一个输入电子邮件地址和密码的文本框以及一个登陆按钮。在点击登陆按钮时,它会检查用户是否已输入电子邮件和密码。如果有任何一个输入为空,它将显示一个警告框。如果输入都不为空,它将执行登陆逻辑,例如调用登陆API。此外,登陆按钮还有一个圆角。
阅读全文