使用 on/off 开关摆脱不同 viewcontroller 中的 UILabel
Using on/off switch to get rid of UILabel in different viewcontroller
我想做的是对 UISwitch
使用 segue,这样我就可以在开关关闭时摆脱第一个视图控制器上名为 phaselabel
的 UILabel
.
在第一个视图控制器上我有:
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
。
这是我要做的:-
我会在 class MeditationScreenViewController
中定义一个变量,它将存储开关的值,无论它是打开还是关闭。
class MeditationScreenViewController: UIViewController {
var isSwitchOn: Bool!
}
并且在调用 prepareForSegue
时,我会将开关值存储到 isSwitchOn
变量,因为在将 viewController 转换为 [ 之后,您可以访问 class 变量=13=].
代码看起来像这样:-
@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
}
}
}
}
最后在 MeditationScreenViewController
中的 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
}
}
在ViewController你要接收的数据
//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
}
我想做的是对 UISwitch
使用 segue,这样我就可以在开关关闭时摆脱第一个视图控制器上名为 phaselabel
的 UILabel
.
在第一个视图控制器上我有:
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
。
这是我要做的:-
我会在 class MeditationScreenViewController
中定义一个变量,它将存储开关的值,无论它是打开还是关闭。
class MeditationScreenViewController: UIViewController {
var isSwitchOn: Bool!
}
并且在调用 prepareForSegue
时,我会将开关值存储到 isSwitchOn
变量,因为在将 viewController 转换为 [ 之后,您可以访问 class 变量=13=].
代码看起来像这样:-
@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
}
}
}
}
最后在 MeditationScreenViewController
中的 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
}
}
在ViewController你要接收的数据
//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
}