我尝试做的是为UISwitch
使用segue,这样当开关关闭时,我可以摆脱第一个视图控制器上UILabel
phaselabel
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
,因此我可以使用它来抓取第一个视图控制器中的HTML:
<button>
<span id="firstspan">Hi</span>
<span id="secondspan">Hello World!!</span>
</button>
CSS:
span {
white-space: nowrap;
}
button {
overflow: hidden;
width:35px;
transition: width 2s;
}
JQuery:
$(function() {
$("#secondspan").hide();
$("button").one("click", function() {
$("button").css("width", "90px");
$("#firstspan").toggle();
$("#secondspan").toggle();
});
});
。
答案 0 :(得分:0)
以下是我要做的事: -
我会在类MeditationScreenViewController
中定义一个变量,它将存储开关的值,无论它是打开还是关闭。
class MeditationScreenViewController: UIViewController {
var isSwitchOn: Bool!
}
并且在调用prepareForSegue
时,我将切换值存储到isSwitchOn
变量,因为在将viewController转换为MeditationScreenViewController
之后,您可以访问类变量。
代码看起来像这样: -
@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
中的MeditationScreenViewController
方法中,使用该值隐藏标签。
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
}
}
答案 1 :(得分:0)
在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
}