[英]Pass label text from one view controller to another using segues only if button (checkbox) is selected
Trying to pass label text from one view controller to another, however I only want the text from selected labels to be passed across. 试图将标签文本从一个视图控制器传递到另一个,但是我只希望将选定标签中的文本传递给其他视图控制器。 The labels are chosen by selecting a check box next to it.
通过选中标签旁边的复选框来选择标签。 My attempt is below.
我的尝试在下面。 I do not receive any errors but I am not seeing the text in the second VC.
我没有收到任何错误,但是在第二个VC中看不到该文本。 It doesn't have to be text off a label I just need to pass text that corresponds with the selected check boxes.
它不必是标签上的文本,我只需要传递与所选复选框相对应的文本即可。 I have tried with just setting a var.
我尝试只设置一个变量。 This is my first attempt at programming so I may be missing something very simple.
这是我第一次尝试编程,因此可能缺少一些非常简单的东西。 In short need to be able to perform a prepare for segue within an 'if' statement.
简而言之,需要能够在“ if”语句中进行准备。
First VC 第一创投
let EnSoString = "Energy Source"
@IBAction func EnergySourceBut(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
} else {
sender.isSelected = true;do {
func prepare(for segue: UIStoryboardSegue, sender: Any){
let receivevc1 = segue.destination as! step4ViewController
receivevc1.EnergySourceLab = EnSoString
}
}
}
}
Second VC 第二创投
//name
var EnergySourceLab:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// received info
if let receivedEnergySourceLab = EnergySourceLab {
EnergySourceLab1.text = receivedEnergySourceLab}
}
Selecting checkbox Code for prepare for segue within button is.selected 选中复选框 以准备在按钮内进行安全检查的代码
Try this: 尝试这个:
First VC 第一创投
let EnSoString = "Energy Source"
@IBAction func EnergySourceBut(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
} else {
self.performSegue(withIdentifier: "segueID", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueID" {
if let vc = segue.destinationViewController as? YourSecondVC {
vc.EnergySourceLab = EnSoString
}
}
Second VC 第二创投
//name
var EnergySourceLab:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// received info
if let receivedEnergySourceLab = EnergySourceLab {
EnergySourceLab1.text = receivedEnergySourceLab}
}
I think below code will help you,Try like this 我认为下面的代码会帮助您,尝试这样
First VC 第一创投
if sender.isSelected == true {
sender.isSelected = false
} else {
sender.isSelected = true
if segue.identifier == "yourIdentifier" {
var vc = segue.destination as! step4ViewController
vc.EnergySourceLab = EnSoString
}
}
Ok so I have figured out a way to solve this issue. 好的,所以我找到了解决此问题的方法。 It's a long and convoluted way to reach my desired outcome but it works.
这是达到我期望的结果的漫长而复杂的方法,但是它有效。 On the first View Controller I made a counter then assigned each check box an Int value.
在第一个View Controller上,我创建了一个计数器,然后为每个复选框分配了一个Int值。 Then I passed that Int value to the next View Controller.
然后,我将该Int值传递给下一个View Controller。 Then in the next View Controller I assigned each Int value to a String value and put that string in the text box (if counter Int value = X print this text, else print "").
然后在下一个View Controller中,我将每个Int值分配给一个String值,并将该字符串放在文本框中(如果counter Int value = X,则打印此文本,否则打印“”)。 I set up multiple counters on the first View Controller so the user can select multiple check boxes and on the second View Controller referenced each of those counters.
我在第一个View Controller上设置了多个计数器,以便用户可以选择多个复选框,并在第二个View Controller上引用了每个计数器。 If anyone is interested I can post the code.
如果有人感兴趣,我可以发布代码。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.