我正在尝试在我的应用程序中添加一个开关。我在故事板中添加了它(带有Switch类和Material模块的IUView。我在我的视图控制器中自定义了它,但是我不知道如何在它上面做动作。
我尝试添加带有touch事件的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")
}
}发布于 2018-11-21 01:13:59
从示例中看,它似乎使用了委托模式。https://github.com/CosmicMind/Samples/blob/master/Projects/Programmatic/Switch/Switch/ViewController.swift
import UIKit
import Material
class ViewController: UIViewController {
// create an outlet and connect it from your storyboard
@IBOutlet weak var mySwitch: Switch!
override func viewDidLoad() {
super.viewDidLoad()
// use this outlet to assign the delegate
mySwitch.delegate = self
}
}
// conform to the protocol
extension ViewController: SwitchDelegate {
// utilise the delegate method
func switchDidChangeState(control: Switch, state: SwitchState) {
print("Switch changed state to: ", .on == state ? "on" : "off")
}
}https://stackoverflow.com/questions/53397793
复制相似问题