【发布时间】:2018-11-20 16:51:27
【问题描述】:
我正在尝试在我的应用中添加一个开关。我在情节提要中添加了它(IUView 与 Switch 类和 Material 模块。我在我的视图控制器中自定义了它,但我不知道如何对其执行操作。
我尝试使用 touch up inside 事件添加 IBAction,但是当我触摸它时我什么都没有。
谢谢。
我的代码:
import Foundation
import UIKit
import Material
class SettingsVC: UIViewController {
@IBOutlet weak var addCalendarSwitch: Switch!
override func viewDidLoad() {
super.viewDidLoad()
addCalendarSwitch.delegate = self
addObserver()
setStyle()
setView()
getStatusAddCalendar()
}
func getStatusAddCalendar(){
if UserDefaults.standard.bool(forKey: "addToCalendar") == true
{
addCalendarSwitch.isOn = true
}
else {
addCalendarSwitch.isOn = false
}
}
}
extension ViewController: SwitchDelegate {
// utilise the delegate method
func switchDidChangeState(control: Switch, state: SwitchState) {
print("Switch changed state to: ", .on == state ? "on" : "off")
}
}
【问题讨论】:
标签: ios xcode cosmicmind