Search code examples
iosswiftuilabelsegueuiswitch

Using on/off switch to get rid of UILabel in different viewcontroller


What I'm trying to do is use a segue for a UISwitch so I can get rid of the UILabel called phaselabel on first view controller when the switch is off.

On first view controller I have:

import UIKit

class MeditationScreenViewController: UIViewController {

   @IBOutlet var phaseLabel: UILabel!

func settingsDidChange(_ settings: MeditationSettings) {
    meditationSettings = settings
    session = MeditationSession(settings: settings, phaseChangedBlock: { [weak self] (phaseName) in
        debugPrint("Entering phase: \(phaseName)")
        guard let strongSelf = self else { return }
        strongSelf.phaseLabel.text = phaseName
        switch phaseName {
        case "Breathe In":
            strongSelf.circleAnimation.animate(direction: CircleAnimationView.Direction.expand, time: TimeInterval(strongSelf.meditationSettings.breathIn))
        case "Breathe Out":
            strongSelf.circleAnimation.animate(direction: CircleAnimationView.Direction.contract, time: TimeInterval(strongSelf.meditationSettings.breathOut))
        default: break
        }
    }, timeChangedBlock: { [weak self] phaseTime, phaseTotal in
        self?.timeLabel.text = "\(Int(phaseTime)) / \(Int(phaseTotal))"
    }, completion: { [weak self] in
        debugPrint("Finished")
        self?.timeLabel.text = ""
        self?.phaseLabel.text = ""
        self?.startStopButton.isSelected = false
    })
}


override func viewWillAppear(_ animated: Bool) {
    self.phaseLabel.text = name
}

On second view controller I have:

import UIKit

var name: String = ""

class MeditationSettingsViewController: UIViewController {

    @IBOutlet var showCycleTitleLabel: UILabel!
    @IBOutlet var showCycleTitleSwitch: UISwitch!

    @IBAction func showCycleTitleChanged(_ sender: UISwitch) {
        if (sender.isOn == true)
        {
            func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
                if segue.identifier == "segue" {
                    if let sendToDetailViewController = segue.destination as? MeditationScreenViewController {
                        name =
        }
        else
        {
            name = ""
        }
    }

I hope this isn't too complicated. I've used the var name: String = "" in second view controller so I can use it to grab the phaseLabel in first view controller.


Solution

  • Here's what I will do:-

    I would define a variable in class MeditationScreenViewController which will store the value of the switch whether it is on or off.

    class MeditationScreenViewController: UIViewController {
    
          var isSwitchOn: Bool!
    }
    

    and when calling the prepareForSegue I will store the switch value to isSwitchOn variable as you have access to the class variables after you cast the viewController as MeditationScreenViewController.

    Code will look something like this:-

     @IBAction func showCycleTitleChanged(_ sender: UISwitch) {
            if (sender.isOn == true)
            {
                func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
                if segue.identifier == "segue" {
                    if let sendToDetailViewController = segue.destination as? MeditationScreenViewController {
                        sendToDetailViewController.isSwitchOn = sender.isOn
    
         }
    }
      }
    }
    

    Finally in your viewWillAppear method in MeditationScreenViewController use the value to hide the label or not.

    override func viewWillAppear(_ animated: Bool) {
    if isSwitchOn == true {
        //unhide the label
       self.phaseLabel.isHidden = false
       //set your label value here
      }
        else {
         self.phaseLabel.isHidden = true
       }
    }