2016年10月3日 星期一

登入頁面(SWIFT3)

今天製作了登入頁面,登入頁面比較簡單沒有太複雜的物件。以下先呈現code

import UIKit

class LoginViewController: UIViewController,UITextViewDelegate,UITextFieldDelegate {
    
    @IBOutlet weak var TopView: UIView!
    @IBOutlet weak var PasswordField: UITextField!
    @IBOutlet weak var UserAccountField: UITextField!
    
    @IBOutlet weak var LogInBtn: UIButton!
    @IBOutlet weak var FB: UIButton!
    @IBOutlet weak var Google: UIButton!
    
    var singleFingerTap:UITapGestureRecognizer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //監聽鍵盤動作
        NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.keyboardWasShown), name: NSNotification.Name.UIKeyboardWillShow, object: nil);
        NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.keyboardWasHidden), name: NSNotification.Name.UIKeyboardWillHide, object: nil);
        singleFingerTap = UITapGestureRecognizer.init(target: self, action: #selector(handleSingleTap));
        
        self.UserAccountField.delegate = self;
        self.PasswordField.delegate = self;
        UserAccountField.clearButtonMode = .always;
        PasswordField.clearButtonMode = .always;
        FB.layer.cornerRadius = 5;
        Google.layer.cornerRadius = 5;
        LogInBtn.layer.cornerRadius = 5;
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.isNavigationBarHidden = true
    }
    
    @IBAction func LoginBtn(_ sender: UIButton) {
        
        if((UserAccountField.text != "") && (PasswordField.text != "")){
            if (self.isValidEmail(testStr: UserAccountField.text!) == false){
                print("信箱格式錯誤");
                let alertcontroller = UIAlertController(title: "Error", message: "The mailbox is malformed", preferredStyle: .alert);
                let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil);
                alertcontroller.addAction(alertAction)
                self.present(alertcontroller, animated: true, completion: nil)
            }else{
                //都有值登入
                print("登入");
                
            }
        }else{
            //帳號密碼為空時
            if(UserAccountField.text == ""){
                UserAccountField.attributedPlaceholder = NSAttributedString(string: "Please enter email", attributes: [NSForegroundColorAttributeName: UIColor.red]);
            }
            if(PasswordField.text == ""){
                PasswordField.attributedPlaceholder = NSAttributedString(string: "Please enter password", attributes: [NSForegroundColorAttributeName: UIColor.red]);
            }
        }
    }
    
    
    
    @IBAction func FBBtn(_ sender: UIButton) {
        
    }
    
    
    @IBAction func GoogleBtn(_ sender: UIButton) {
        
    }
    
    // MARK: e-Mail檢查
    func isValidEmail(testStr:String) -> Bool {
        // print("validate calendar: \(testStr)")
        let stricterFilter:Bool = false;
        
        let stricterFilterString:String = "[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}"
        let laxString:String = ".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*"
        let emailRegex:String = stricterFilter ? stricterFilterString : laxString;
        let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegex)
        return emailTest.evaluate(with: testStr)
    }
    
    // MARK: TEXTField按下鍵盤return後動作
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        if(textField.isEqual(UserAccountField)){
            PasswordField.becomeFirstResponder()
        }else{
            self.LoginBtn(.init(type: .custom))
        }
        return true;
    }
    //MARK: 鍵盤監聽動作(當用戶點view收起)
    func keyboardWasShown(){
        TopView.addGestureRecognizer(singleFingerTap);
    }
    func keyboardWasHidden(){
        TopView.removeGestureRecognizer(singleFingerTap);
    }
    func handleSingleTap(){
        //Do stuff here...
        UserAccountField.resignFirstResponder()
        PasswordField.resignFirstResponder()

    }


以上照著做就可以了喔!!測試圖片如下

下圖為按下login後print出來登入


下圖為判斷帳號(email)格式錯誤

希望能幫助到大家喔!!





沒有留言:

張貼留言