2016年12月25日 星期日

Swift3 簡單的鍵盤監控及AlertAction凍結

這邊就不多做解釋了,直接放上Code跟截圖跟大家分享!!


storyboard自行發揮摟~而我為了呈現會擋住textfield所以我故意放比較低

在這要比較注意的是,通常大多數會擋住textfield是因為沒有使用tableview
物件無法滑動的情況下就變成必須改變view的座標,但以下寫法並不適合過多欄位使用。



import UIKit

class ViewController: UIViewController ,UITextFieldDelegate{

    @IBOutlet weak var ShowInputLabel: UILabel!
    @IBOutlet weak var YouerTextField: UITextField!
    @IBOutlet weak var AlertTextLabel: UILabel!
    
    var singleFingerTap:UITapGestureRecognizer!
    var alert:UIAlertController!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.YouerTextField.delegate = self

        //鍵盤監聽
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWasShow), name: .UIKeyboardWillShow, object: nil);
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWasHidden), name: .UIKeyboardWillHide, object: nil);
        singleFingerTap = UITapGestureRecognizer.init(target: self, action: #selector(handleSingleTap));
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        NotificationCenter.default.removeObserver(self)
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func AlertBtn(_ sender: UIButton) {
         alert = UIAlertController(title: "Alert", message: "輸入下方欄位", preferredStyle: .alert);
        //加入textfieldalert
        alert.addTextField { (text:UITextField) in
            text.placeholder = "輸入"
           //監聽
            NotificationCenter.default.addObserver(self, selector: #selector(ViewController.alertCheck(notification:)), name: .UITextFieldTextDidChange, object: text);
        }
        let alertAction = UIAlertAction(title: "OK", style: .default) { (UIAlertAction) in
            self.AlertTextLabel.text = self.alert.textFields?.first?.text
            //釋放間聽
            NotificationCenter.default.removeObserver(self, name: .UITextFieldTextDidChange, object: nil);
        }
        
        alertAction.isEnabled = false//凍結OK
        alert.addAction(alertAction)
        self.present(alert, animated: true, completion: nil);
    }
    //凍結action(監控)
    func alertCheck(notification:Notification){
        alert = self.presentedViewController as! UIAlertController?
        if(alert != nil){
            let input = (alert.textFields?.first)! as UITextField
            let alertActionOK = alert.actions.last! as UIAlertAction
            //計算字元到多少未解凍
            alertActionOK.isEnabled = (input.text?.characters.count)! > 5
        }
    }
    
    @IBAction func OutPutBtn(_ sender: UIButton) {
        ShowInputLabel.text = YouerTextField.text
    }
//MARK: 鍵盤監聽動作(點view隱藏鍵盤)
    //鍵盤顯示
    func keyboardWasShow(notification:Notification){
        self.view.addGestureRecognizer(singleFingerTap)
        
        //獲取鍵盤高度
        let info = notification.userInfo!
        let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let hieght = keyboardFrame.height
        //改變view座標避免鍵盤擋到textfield(注意:欄位多時不適用)
       self.view.frame.origin.y = -hieght
    }
    //鍵盤收起
    func keyboardWasHidden(notification:Notification){
        self.view.removeGestureRecognizer(singleFingerTap)
        self.view.frame.origin.y = 0
        
    }
    func handleSingleTap(){
        YouerTextField.resignFirstResponder()
    }
    //MARK: 鍵盤按下return後的動作
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        if(textField.text != ""){
            self.OutPutBtn(.init(type:.custom))
        }
        //假設如果有多個欄位要動作時
        /*if(textField.isEqual("第一個欄位")){
            YouerTextField.becomeFirstResponder()//第二個欄位
        }else{
            self.OutPutBtn(.init(type:.custom))
        }*/
        
        return true
    }


}

出來的效果如圖:





沒有留言:

張貼留言