创建LoginViewController 继承UIViewController
创建User继承Nsobject
导入图片]
在Appdelegate中
//实例化登录控制器
let loginVC = LoginViewController()
//做成窗口的跟视图控制器
self.window?.rootViewController = loginVC
return true
在LoginViewController 中
let scrWidth = UIScreen.main.bounds.size.width //屏幕宽
let scrHeight = UIScreen.main.bounds.size.height //屏幕高
class LoginViewController: UIViewController,UITextFieldDelegate {
//定义数组属性
var userArr:[User] = []
//定义控件shuxing
var backImageView:UIImageView? //背景图
var accountTF:UITextField? //文本输入框
var pwdTf:UITextField? //密码输入框
var loginBtn:UIButton? //登录按钮
var thirdLoginView:UIView? //第三方登录视图
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.backImageView = UIImageView(frame: CGRect(x:0,y:0, width:scrWidth,height:scrHeight))
self.backImageView?.image = UIImage(named: "登录")
self.view.addSubview(self.backImageView!)
if let bgV = self.backImageView{
self.view.addSubview(bgV)
}
//账号输入框
self.accountTF = UITextField(frame: CGRect(x: 20, y: 120, width: scrWidth - 40, height: 65))
//设置背景图
self.accountTF?.background = UIImage(named: "3")
//设置提示文字
self.accountTF?.placeholder = "请输入账号"
self.accountTF? .setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
//tag值
self.accountTF?.tag = 100
// 设置居中
self.accountTF?.textAlignment = NSTextAlignment.center
//
//设置输入文字的颜色
self.accountTF?.textColor = UIColor.white
//
// //设置输入文字的字体
self.accountTF?.font = UIFont.systemFont(ofSize: 22)
//
//设置代理
self.accountTF?.delegate = self
self.view.addSubview(self.accountTF!)
//=============================密码
//账号输入框
self.pwdTf = UITextField(frame: CGRect(x: 20, y: 190, width: scrWidth - 40, height: 65))
//设置背景图
self.pwdTf?.background = UIImage(named: "7")
//tag值
self.accountTF?.tag = 100
//设置提示文字
self.pwdTf?.placeholder = "请输入密码"
// //设置带样式的文本
// //做一个格式字典
// var attributeDic:[NSAttributedStringKey:Any]
//
// self.accountTF?.attributedPlaceholder = NSAttributedString(
//
self.pwdTf? .setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
// 设置居中
self.pwdTf?.textAlignment = NSTextAlignment.center
//
//设置输入文字的颜色
self.pwdTf?.textColor = UIColor.white
//
// //设置输入文字的字体
self.pwdTf?.font = UIFont.systemFont(ofSize: 19)
//
//设置代理
self.pwdTf?.delegate = self
//
//设置密文输入效果
self.pwdTf?.isSecureTextEntry = true
//设置数字键盘
self.pwdTf?.keyboardType = .numberPad
//添加子视图
self.view.addSubview(self.pwdTf!)
//===================================登录按钮
self.loginBtn = UIButton(frame: CGRect(x: 40, y: 260, width: scrWidth - 60, height: 80))
//设置背景图
self.loginBtn?.setBackgroundImage(UIImage(named: "11"),for: .normal)
//设置标题
self.loginBtn?.setTitle("登录", for: .normal)
//设置标题文本颜色
self.loginBtn?.setTitleColor(UIColor.white, for: .normal)
self.view.addSubview(self.loginBtn!)
//添加点击事件
self.loginBtn?.addTarget(self, action: #selector(loginBtnDidPress(sender:)), for: .touchUpInside)
//=================================开关控件
let switchBtn = UISwitch(frame: CGRect(x: 100, y: 400,
width:50, height: 50))
switchBtn.onTintColor = UIColor.red
switchBtn.setOn(false, animated: true)
switchBtn.addTarget(self, action: #selector(switchBtnHandel(sender:)), for: .valueChanged)
self.view.addSubview(switchBtn)
}
//开关触发方法
@objc func switchBtnHandel(sender:UISwitch) -> Void {
if sender.isOn {
self.loginBtn?.isHidden = true
}
else{
self.loginBtn?.isHidden = false
}
}
//定义按钮触发的方法
@objc func loginBtnDidPress(sender:UIButton) -> Void {
//如果用户没有输入账号和密码 提示框
if (self.accountTF?.text?.isEmpty)! || (self.pwdTf?.text?.isEmpty)! {
//实例化控制器
let alertCtl = UIAlertController(title: nil, message: "账号密码不可为空", preferredStyle: .alert)
//设置取消按钮操作
let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
//将按钮添加到控制器上
alertCtl.addAction(cancelAction)
//弹出控制器
self.present(alertCtl,animated: true, completion:nil)
return;
}
//将用户输入的数据封装为Model对象
let oneUser = User(account: (self.accountTF?.text)!, password: (self
.pwdTf?.text)!)
self.userArr.append(oneUser)
//遍历数组 查看之前登录的所有用户
for item in self.userArr{
print(item)
}
}
//点击键盘的return按钮触发的回调方法
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
//文本将要变化触发的回调 在此方法中可以限定文本的长度
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//做一个可变字符串
let str = textField.text
let newStr = (str as! NSString).replacingCharacters(in: range, with: string)
//如果是账号输入框,允许显示当前输入的字符
if textField.tag == 100 {
return true
}
//如果是密码输入框,不允许显示当前输入的字符
else{
return (newStr.characters.count) <= 12
}
}
//点击view触发的回调方法
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
//
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
在User 中
var account = ""
var password = ""
init(account acc:String,password pwd:String) {
account = acc
password = pwd
}
//添加自我描述的方法
override var description:String{
return "账号:\(account),密码:\(password)"
}
网友评论