CityViewController

let ScrW = UIScreen.main.bounds.size.width

let ScrH = UIScreen.main.bounds.size.height

提示框

extension UIViewController

{

    func showAlert(msg:String,sec:TimeInterval)  {

        //实例化弹出控制器

        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="请输入城市名字"

        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("点击查询", 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)

        }

        //实例化结果控制器

        let resultVC = CityResultViewController()

        //传递数据

        resultVC.passString=CityTF!.text!

        //控制器跳转

        self.navigationController?.pushViewController(resultVC, animated:true)

    }

    // MARK: - ----------- UITextFieldDelegate -------

    //点击reture按钮回调

    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)

    }

}

你可能感兴趣的:(CityViewController)