0

i'm trying to add a switch in my app. I added it in the storyboard (IUView with Switch class and Material module. I customed it in my viewcontroller but I don't know how to do the action on it.

I tried adding IBAction with the touch up inside event, but I have nothing when I touch it.

Thanks.

My code:

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

1 Answers1

0

Looking at the examples it seems it is using a delegate pattern. 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")
    }
}
Peter Pajchl
  • 2,699
  • 21
  • 24
  • Yes I saw this, but I'm novice, and I don't know how to do this with my switch in the storyboard – Ludivine Fafournoux Nov 21 '18 at 08:33
  • I've updated the example. To access the switch from storyboard within your controller you need to create an `IBOutlet` and connect it. Then you can assign the delegate and provide the delegate methods as shown. – Peter Pajchl Nov 21 '18 at 09:41
  • Ok thanks, I tried, but first, I have to make public the function switchDidChangeState " Method 'switchDidChangeState(control:state:)' must be declared public because it matches a requirement in public protocol 'SwitchDelegate' ", and if I put mySwitch.delegate = self, self is my view Controller and I have this error " Cannot assign value of type 'SettingsVC' to type 'SwitchDelegate?' " – Ludivine Fafournoux Nov 21 '18 at 10:34
  • Unfortunately without seeing any of your code it is hard to provide help. I've created a simple example with storyboard if you would like to review/compare your implementation. https://transfernow.net/279i98h0g14f – Peter Pajchl Nov 21 '18 at 12:00
  • Thanks, I compared with my code and I think I have the same thing. I've edit my query with my code. With that code, I have the two issue I wrote before. – Ludivine Fafournoux Nov 21 '18 at 13:08
  • in your code you are doing `extension ViewController: SwitchDelegate {` - it should be like this instead `extension SettingsVC: SwitchDelegate {` as you are extending your custom class – Peter Pajchl Nov 21 '18 at 13:17
  • Thanks a lot. Now, I have other problem nothing happen when I touch the switch, but I try to find – Ludivine Fafournoux Nov 21 '18 at 14:03
  • I don't know why it doesn't work in my view. It's a scrollview, in it there is a view and inside my switch. – Ludivine Fafournoux Nov 21 '18 at 15:06
  • I did similar setup again to compare https://transfernow.net/810fr2w1t6b3 - if you are able to share your project I could have a look... – Peter Pajchl Nov 21 '18 at 15:45
  • Thanks, I can share my project, but i've added screenshot, tell me if you want more. I compare with your project but I can't find. – Ludivine Fafournoux Nov 21 '18 at 16:01
  • The project would be easier but to confirm; right now when you run the app you can see the switch but interaction (touch) does not work (does not toggle state)? If so I would check if any of the views in your hierarchy don't have `userInteractionEnabled` set to false. – Peter Pajchl Nov 21 '18 at 16:11
  • Sorry I did a mistake, I can't share my project. You're right when I touch the switch nothing happens. I checked if `userInteractionEnabled` was set to false in a view but no. – Ludivine Fafournoux Nov 21 '18 at 16:30
  • And the switch is enabled `addCalendarSwitch.isEnabled = true` or in interface builder set to `default` or `on` right? – Peter Pajchl Nov 21 '18 at 16:55
  • I am afraid I am out of options to help. I would suggest to try to simplify first (debug) by moving the switch into simpler view hierarchy and see if things work. Your view setup is IMHO a bit cumbersome as normally you would use table view or similar for this kind of layout. – Peter Pajchl Nov 21 '18 at 17:08
  • Hi, so I found the problem, one of the parent's view had its height set to 0. This was made by the previous dev, and I didn't think about checking there. Thanks for your help – Ludivine Fafournoux Nov 22 '18 at 09:57