let ScrW = UIScreen.main.bounds.size.width
let ScrH = UIScreen.main.bounds.size.height
提示框
extension UIViewController
{
? ? func showAlert(msg:String,sec:TimeInterval)? {
? ? ? ? //實(shí)例化彈出控制器
? ? ? ? let alertVC =UIAlertController(title:nil, message: msg, preferredStyle: .alert)
? ? ? ? self.present(alertVC, animated:true, completion:nil)
? ? ? ? self.perform(#selector(hideAlertVC(sender:)), with: alertVC, afterDelay: sec)
? ? }
? ? @objc func hideAlertVC(sender:UIAlertController)? {
? ? ? ? sender.dismiss(animated:true, completion:nil)
? ? }
}
class CityViewController:UIViewController,UITextFieldDelegate{
? ? varCityTF :UITextField?
? ? varCityBtn :UIButton?
? ? override funcviewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? self.view.backgroundColor = UIColor.white
? ? ? ? CityTF=UITextField(frame:CGRect(x:0, y:0, width:200, height:50))
? ? ? ? CityTF?.center=CGPoint(x:ScrW/2, y:200)
? ? ? ? CityTF?.borderStyle= .line
? ? ? ? CityTF?.placeholder="請(qǐng)輸入城市名字"
? ? ? ? CityTF?.textColor=UIColor.blue
? ? ? ? CityTF?.textAlignment= .center
? ? ? ? CityTF?.delegate=self
? ? ? ? self.view.addSubview(CityTF!)
? ? ? ? CityBtn=UIButton(frame:CGRect(x:0, y:0, width:100, height:50))
? ? ? ? CityBtn?.center=CGPoint(x:ScrW/2, y:300)
? ? ? ? CityBtn?.backgroundColor = UIColor.black
? ? ? ? CityBtn?.setTitleColor(UIColor.white, for: .normal)
? ? ? ? CityBtn?.setTitle("點(diǎn)擊查詢", for: .normal)
? ? ? ? CityBtn?.addTarget(self, action:#selector(btnDidPress(sender:)), for: .touchUpInside)
? ? ? ? self.view.addSubview(CityBtn!)
? ? }
? ? @objc func btnDidPress(sender:UIButton)? {
? ? ? ? //如果信息為空給客戶提示
? ? ? ? if(CityTF?.text?.isEmpty)!
? ? ? ? {
? ? ? ? ? ? self.showAlert(msg:"信息不可為空", sec:2.0)
? ? ? ? }
? ? ? ? //實(shí)例化結(jié)果控制器
? ? ? ? let resultVC = CityResultViewController()
? ? ? ? //傳遞數(shù)據(jù)
? ? ? ? resultVC.passString=CityTF!.text!
? ? ? ? //控制器跳轉(zhuǎn)
? ? ? ? self.navigationController?.pushViewController(resultVC, animated:true)
? ? }
? ? // MARK: - ----------- UITextFieldDelegate -------
? ? //點(diǎn)擊reture按鈕回調(diào)
? ? func textFieldShouldReturn(_textField:UITextField) ->Bool{
? ? ? ? textField.resignFirstResponder()
? ? ? ? return true
? ? }
? ? // MARK: - ----------- touches Methods -------
? ? override func touchesEnded(_touches:Set, with event:UIEvent?) {
? ? ? ? super.touchesEnded(touches, with: event)
? ? ? ? CityTF?.resignFirstResponder()
? ? ? ? //將view及 其子視圖都放棄編輯
? ? ? ? self.view.endEditing(true)
? ? }
}