Swift UIAlertController、UISegmentedControl

// MARK: 提示框

    func _initAlertView() {

        

        let btn = UIButton(type: UIButtonType.ContactAdd)

        btn.frame = CGRect(x: 100, y: 150, width: 50, height: 50)

        btn.addTarget(self, action: "showAlert", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(btn)

    }

    

    func showAlert() {

//        let alertView = UIAlertView(title: "警告", message: "密码错误", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "确定")

////        let alertView = UIAlertView(title: "警告", message: "密码错误", delegate: self, cancelButtonTitle: "取消")

//        

//        //设置alertView的样式

//        alertView.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput

//        alertView.show()

        

        

        let alertCtrl = UIAlertController(title: "温馨提示", message: "输入账号和密码", preferredStyle: UIAlertControllerStyle.Alert)

        alertCtrl.addTextFieldWithConfigurationHandler { (textField) -> Void in

            textField.placeholder = "输入账号"

        }

 

        alertCtrl.addTextFieldWithConfigurationHandler { (textField) -> Void in

            textField.placeholder = "输入密码"

            textField.secureTextEntry = true

        }

        let sureAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Destructive) { (alertAction) -> Void in

            let tfs: [UITextField] = alertCtrl.textFields!

            let tf1: UITextField = tfs[0]

            let tf2: UITextField = tfs[1]

            print("确定:\(alertAction.title)")

            print(tf1.text)

            print(tf2.text)

        }

        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Destructive) { (alertAction) -> Void in

            print("取消:\(alertAction.title)")

        }

        

        alertCtrl.addAction(sureAction)

        alertCtrl.addAction(cancelAction)

        self.presentViewController(alertCtrl, animated: true, completion: nil)

    }

    

    

    // MARK: 提示界面

    func _initActionSheet() {

        let btn = UIButton(type: UIButtonType.InfoDark)

        btn.frame = CGRect(x: 200, y: 150, width: 50, height: 50)

        btn.addTarget(self, action: "showActionSheet", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(btn)

    }

    

    func showActionSheet() {

//        let actionSheet = UIActionSheet(title: "提示", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "确定", otherButtonTitles: "按钮1", "按钮2")

//        actionSheet.showInView(self.view)

        let alertCtrl = UIAlertController(title: "提示", message: "重点提示Message", preferredStyle: UIAlertControllerStyle.ActionSheet)

        let sureAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (alertAction) -> Void in

            print("确定:\(alertAction.title)")

        }

        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel) { (alertAction) -> Void in

            print("取消:\(alertAction.title)")

        }

        alertCtrl.addAction(sureAction)

        alertCtrl.addAction(cancelAction)

        self.presentViewController(alertCtrl, animated: true, completion: nil)

    }

    // MARK: UISegmentedControl 两种创建方式都实用

    func _initsegmentControl() {

        

// 1.0 设置按钮的标题数组

        let array: [String] = ["选择","搜索","工具"]

        // 1.1 创建分段控件

        let segmentCtrl = UISegmentedControl(items: array)

        segmentCtrl.frame = CGRect(x: 100, y: 100, width: 150, height: 25)

        // 1.2 设置默认选中按钮

        segmentCtrl.selectedSegmentIndex = 0

        segmentCtrl.addTarget(self, action: "segmentAction:", forControlEvents: UIControlEvents.ValueChanged)

        self.view.addSubview(segmentCtrl)

        

        // 2.0 设置按钮的标题数组

        let arrayString:[String] = ["11","22","33"];

        // 2.1 创建分段控件

        let segmentContrl = UISegmentedControl(items: arrayString)

        segmentContrl.frame = CGRectMake(100, 200, 200, 50)

        // 2.2 设置默认选中按钮

        segmentContrl.selectedSegmentIndex = 2

        segmentContrl.addTarget(self, action: "segmentAction:", forControlEvents: UIControlEvents.ValueChanged)

        self.view.addSubview(segmentContrl)

    }

    

    func segmentAction(segment: UISegmentedControl) {

        

       print(segment.selectedSegmentIndex)

        switch segment.selectedSegmentIndex{

        case 0:

            print("选择")

        case 1:

           print("搜索")

        case 2:

           print("工具")

        default: break

            

        }

    }

 

posted @ 2016-07-22 14:33  光光96  阅读(268)  评论(0编辑  收藏  举报