CityViewController

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)

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容