代码之家  ›  专栏  ›  技术社区  ›  user7186765

在不同的viewcontroller中使用开/关开关消除UILabel

  •  0
  • user7186765  · 技术社区  · 7 年前

    我要做的是用segue表示 UISwitch 这样我就可以摆脱 UILabel phaselabel 当开关关闭时,打开第一视图控制器。

    在first view controller上,我有:

    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
    }
    

    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 = ""
            }
        }
    

    var name: String = "" 在第二个视图控制器中,以便我可以使用它来抓取 phaseLabel 在第一视图控制器中。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Dark Innocence    7 年前

    以下是我将要做的:-

    我会在课堂上定义一个变量 MeditationScreenViewController 它将存储开关的值,无论开关是打开还是关闭。

    class MeditationScreenViewController: UIViewController {
    
          var isSwitchOn: Bool!
    }
    

    当呼叫 prepareForSegue isSwitchOn 变量,因为您可以在将viewController转换为后访问类变量 冥想屏幕控制器 .

    代码如下所示:-

     @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
    
         }
    }
      }
    }
    

    终于在你的 viewWillAppear 冥想屏幕控制器

    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
       }
    }
    
        2
  •  0
  •   flexr    7 年前

    //create variable to store passed data from UISwitch
    var switchStatePassed = [String]()
    
    //perform tasks with passed data
    

    在另一个ViewController中

    fun prepare(for segue: UIStorybordSegue, sender: Any?) {
      //set destination to MeditationScreenViewController
      if let destination = segue.destination as? 
      MeditationScreenViewController {
       //check UISwitch state and create variable to store info
       if showCycleTitleSwitch.on {
        let switchState = "enabled"
       } else {
          let switchState = "disabled"
        }
       //send switchState to other ViewController
       destination.switchStatePassed = switchState
    }