简体   繁体   English

仅当选择按钮(复选框)时,才使用segues将标签文本从一个视图控制器传递到另一个视图控制器

[英]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.

相关问题 如何使用segues将字典数据从一个视图控制器发送到另一个视图控制器? - how to send the dictionary data from one view controller to another view controller using segues? 我正在尝试使用segues展开时将字符串从一个视图控制器传递到另一视图控制器,但我无法这样做 - I am trying to pass a string from one view controller to another view controller while unwinding using segues and I am not able to do so 从一个 UIButton 到同一个视图控制器或另一个创建多个 segue - Creating multiple segues from one UIButton to the same view controller or another 如何通过一个按钮动作将一个视图控制器隐藏中的tableview传递给另一个视图控制器 - how to pass tableview from one view controller hide to another view controller unhide using button action 如何不使用segues将数据从一个viewController传递到另一个? - How to pass data from one viewController to another without using segues? 使用按钮操作将数据从一个视图控制器传递到另一个视图控制器的方法是什么? - What are the methods to pass forward data from one View Controller to another using button action? 将信息从tableview传递给视图控制器而不是基于indexPathForSelectedRow,而是传递给选定单元格的标签文本? - Pass information from a tableview to a view controller based not on indexPathForSelectedRow, but on label text of cell selected? 使用按钮从一个视图控制器过渡到另一个 - transitioning from one view controller to another using a button 将NSManagedObject从一个视图控制器传递到另一个视图控制器 - Pass NSManagedObject from one view controller to another 两个segue从一个视图控制器传递相同的数据 - Two segues passing the same data from one view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM