实现效果
首先进入初始界面,输入账号Linchuantao,密码Linchuantao,显示登录失败(如下左图),因此需要进行注册,点击左下角注册,输入账号Linchuantao,密码Linchuantao,点击注册 之后再次通过账号Linchuantao,密码Linchuantao进行登录,登录成功
源代码
登录界面代码 ViewController.swift
import UIKit
class ViewController: UIViewController {
let userNameLabel = UILabel(frame:CGRect(x:50,y: 300, width: 200, height: 50))
let passwordLabel = UILabel(frame:CGRect(x:50,y: 385, width: 100, height: 50))
let enterUser = UITextField(frame: CGRect(x: 140, y: 310, width: 200, height: 40))
let enterPassword = UITextField(frame: CGRect(x: 140, y: 390, width: 200, height: 40))
var user = Dictionary<String,String>()
let loginBtn = UIButton(type: .system)
let regBtn = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
userNameLabel.text = "用户名"
passwordLabel.text = "密码"
userNameLabel.textColor=UIColor.darkGray
passwordLabel.textColor=UIColor.darkGray
self.view.addSubview(userNameLabel)
self.view.addSubview(passwordLabel)
enterUser.borderStyle = UITextField.BorderStyle.roundedRect
enterPassword.borderStyle = UITextField.BorderStyle.roundedRect
enterPassword.isSecureTextEntry = true
enterUser.placeholder="请输入用户名"
enterPassword.placeholder="请输入密码"
enterUser.clearButtonMode=UITextField.ViewMode.whileEditing
enterPassword.clearButtonMode=UITextField.ViewMode.whileEditing
self.view.addSubview(enterUser)
self.view.addSubview(enterPassword)
loginBtn.frame = CGRect(x: 85, y:450, width: 100, height: 50)
regBtn.frame = CGRect(x: 200, y:450, width: 100, height: 50)
loginBtn.setTitle("登录", for: .normal)
loginBtn.addTarget(self, action: #selector(ViewController.mapForLogin(_:)), for: UIControl.Event.touchUpInside)
regBtn.setTitle("注册", for: .normal)
self.view.addSubview(loginBtn)
regBtn.addTarget(self, action: #selector(ViewController.openViewController), for: .touchUpInside)
self.view.addSubview(regBtn)
}
@objc func openViewController()
{
let loginView = ViewController1()
loginView.viewController = self
self.present(loginView, animated: true, completion: nil)
}
@objc func mapForLogin(_ button:UIButton)
{
if user[enterUser.text!]==enterPassword.text{
let alertController = UIAlertController(title: "登录成功!",message: nil, preferredStyle: .alert)
self.present(alertController, animated: true,completion: nil)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {self.presentedViewController?.dismiss(animated: false, completion: nil)}
}
else{
let alertController = UIAlertController(title: "登录失败!",message: nil, preferredStyle: .alert)
self.present(alertController, animated: true,completion: nil)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {self.presentedViewController?.dismiss(animated: false, completion: nil)}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
注册界面代码
import UIKit
class ViewController1: UIViewController {
let userNameLabel = UILabel(frame:CGRect(x:50,y: 100, width: 200, height: 50))
let passwordLabel = UILabel(frame:CGRect(x:50,y: 180, width: 100, height: 50))
let enterUser = UITextField(frame: CGRect(x: 140, y: 110, width: 200, height: 40))
let enterPassword = UITextField(frame: CGRect(x: 140, y: 190, width: 200, height: 40))
let regBtn = UIButton(type: .system)
let returnBtn = UIButton(type: .system)
weak var viewController : ViewController?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
userNameLabel.text = "用户名"
passwordLabel.text = "密码"
userNameLabel.textColor=UIColor.darkGray
passwordLabel.textColor=UIColor.darkGray
self.view.addSubview(userNameLabel)
self.view.addSubview(passwordLabel)
enterUser.borderStyle = UITextField.BorderStyle.roundedRect
enterPassword.borderStyle = UITextField.BorderStyle.roundedRect
enterPassword.isSecureTextEntry = true
enterUser.placeholder="请输入用户名"
enterPassword.placeholder="请输入密码"
enterUser.clearButtonMode=UITextField.ViewMode.whileEditing
enterPassword.clearButtonMode=UITextField.ViewMode.whileEditing
self.view.addSubview(enterUser)
self.view.addSubview(enterPassword)
regBtn.frame = CGRect(x: 100, y:250, width: 100, height: 50)
regBtn.setTitle("注册", for: .normal)
regBtn.addTarget(self, action: #selector(ViewController1.dismissSelf), for: .touchUpInside)
self.view.addSubview(regBtn)
returnBtn.frame = CGRect(x: 200, y:250, width: 100, height: 50)
returnBtn.setTitle("返回", for: .normal)
for: .touchUpInside)
self.view.addSubview(returnBtn)
}
@objc func dismissSelf()
{
viewController?.user.updateValue(enterPassword.text!, forKey: enterUser.text!)
self.dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
|