没啥好写的基于第一节的内容添加用户名框,密码框,登陆按钮,以及简单的登陆判断。
//
// LoginViewController.swift
// cyshop
//
// Created by duan on 2022/10/7.
//
import UIKit
class LoginViewController: UIViewController {
var deviceSize = UIScreen.main.bounds;
var userNameTextField = UITextField();
var passWorldTextField = UITextField();
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.orange;
userNameTextField = addUserNameView();
passWorldTextField = addPassWorldView();
addSubmitButton();
}
func addUserNameView()->UITextField{
let view = UIView(frame: CGRect.init(x: (deviceSize.width-250)*0.5, y: (deviceSize.height)*0.5-100, width: 250, height: 40));
//view.backgroundColor = UIColor.blue;
let label = UILabel(frame: CGRect.init(x: 0, y: 0, width: 40, height: 40));
label.text = "账号:";
let userNameTextField = UITextField(frame: CGRect.init(x: 50, y: 0, width: 200, height: 40));
userNameTextField.textColor = UIColor.black;
userNameTextField.borderStyle = UITextField.BorderStyle.roundedRect;
view.addSubview(label);
view.addSubview(userNameTextField);
self.view.addSubview(view);
return userNameTextField;
}
func addPassWorldView()->UITextField{
let view = UIView(frame: CGRect.init(x:(deviceSize.width-250)*0.5, y: (deviceSize.height)*0.5-50, width: 250, height: 40));
//view.backgroundColor = UIColor.blue;
let label = UILabel(frame: CGRect.init(x: 0, y: 0, width: 40, height: 40));
label.text = "密码:";
let passWorldTextField = UITextField(frame: CGRect.init(x: 50, y: 0, width: 200, height: 40));
passWorldTextField.textColor = UIColor.black;
passWorldTextField.borderStyle = UITextField.BorderStyle.roundedRect;
view.addSubview(label);
view.addSubview(passWorldTextField);
self.view.addSubview(view);
return passWorldTextField;
}
func addSubmitButton(){
//创建圆角按钮
let button: UIButton = UIButton(type: .roundedRect);
button.frame = CGRectMake((deviceSize.width-250)*0.5,(deviceSize.height)*0.5,250,40);
button.backgroundColor = UIColor.black;
button.setTitle("登陆", for: .normal);
button.setTitleColor(UIColor.white, for: .normal);
button.layer.cornerRadius = 8;
button.addTarget(self, action: #selector(loginPressed), for: .touchUpInside);
self.view.addSubview(button);
}
// 登陆按钮点击事件
@objc private func loginPressed() {
let userName = NSString(string: userNameTextField.text ?? "");
let password = NSString(string: passWorldTextField.text ?? "");
var message = "";
if(userName == "admin" && password == "111"){
message = "登陆成功"
}else{
message = "登陆失败"
}
let alertController = UIAlertController(title: message,message: nil, preferredStyle: .alert)
let okAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
git地址:https://gitee.com/benovel/cyshop/tree/2%E7%94%B5%E5%95%86%E7%B3%BB%E7%BB%9FiOS-%E7%AE%80%E5%8D%95%E7%9A%84%E6%B3%A8%E5%86%8C%E7%99%BB%E9%99%86/