IOS控制元件-UISwitch開關控制元件的使用
阿新 • • 發佈:2018-11-16
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) }