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.
Here's what I will do:-
I would define a variable in class
MeditationScreenViewControllerwhich will store the value of the switch whether it is on or off.and when calling the
prepareForSegueI will store the switch value toisSwitchOnvariable as you have access to the class variables after you cast the viewController asMeditationScreenViewController.Code will look something like this:-
Finally in your
viewWillAppearmethod inMeditationScreenViewControlleruse the value to hide the label or not.