首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在有物料的交换机上执行操作(CosmicMind)

如何在有物料的交换机上执行操作(CosmicMind)
EN

Stack Overflow用户
提问于 2018-11-21 00:51:27
回答 1查看 84关注 0票数 0

我正在尝试在我的应用程序中添加一个开关。我在故事板中添加了它(带有Switch类和Material模块的IUView。我在我的视图控制器中自定义了它,但是我不知道如何在它上面做动作。

我尝试添加带有touch事件的IBAction,但是当我触摸它的时候什么都没有。

谢谢。

我的代码:

代码语言:javascript
复制
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")
    }
}

hierarchy

screen of my view

EN

回答 1

Stack Overflow用户

发布于 2018-11-21 01:13:59

从示例中看,它似乎使用了委托模式。https://github.com/CosmicMind/Samples/blob/master/Projects/Programmatic/Switch/Switch/ViewController.swift

代码语言:javascript
复制
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")
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53397793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档