ios - 如何使用 Material (CosmicMind) 实现开关动作
问题描述
我正在尝试在我的应用程序中添加一个开关。我将它添加到情节提要中(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")
}
}
解决方案
查看示例,它似乎正在使用委托模式。 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")
}
}
推荐阅读
- postgresql - Golang Postgres Squirrel 语法错误 (SQLSTATE 42601)
- r - 如何使用 processR 包调整有节制的调解模型?
- python - 如何在该程序中实施财务功能?
- javascript - Axios 不向表中插入值
- python - 使用 Python 获取一些受保护文件系统窗口的副本?
- swiftui - 无法检索区域信息 - 监控不起作用
- c++ - 为什么编译器允许默认构造函数使用引用成员初始化类
- flex-lexer - 带有自动机的柔性扫描仪推回溢出
- java - Tomcat 服务器无响应“http-nio-80-Acceptor-0”
- python - 可能导致此错误的原因:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 568: invalid start byte