IOS控件-UISwitch开关控件的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21153627/article/details/84069984

UISwitch开关控件的使用

func test3() {
        //创建一个显示区域
        let rect = CGRect(x: 130, y: 100, width: 0, height: 0);
        //初始化开关对象。设置大小位置
        let  uiSwitch = UISwitch(frame: rect);
        //设置开关的默认状态为选中
        uiSwitch.setOn(true, animated: true);
        //给开关对象 设置状态监听
        uiSwitch.addTarget(self, action: #selector(ViewController.test3OnClick(_ :)), for: UIControlEvents.valueChanged);
        self.view.addSubview(uiSwitch);
        
        
    }
    @objc func test3OnClick(_ sender:UISwitch){
        var  str = "Turn on the switch";
        if (!sender.isOn) {
            str="Ture off the switch";
        }
        let dialog = UIAlertController(title: "switch state", message: str, preferredStyle: UIAlertControllerStyle.alert);
        let dialogAction=UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil);
        dialog.addAction(dialogAction);
        self.present(dialog, animated: true, completion: nil)
        
        
    }

猜你喜欢

转载自blog.csdn.net/qq_21153627/article/details/84069984