如何在一个发送者中发送多个变量?

How to send multiple variables in one single sender?

我试图在一个发送器中发送多个变量,以在 Viewcontroller 中显示它已连接到名为 menuENG 的 segue。我有五个按钮,每个按钮应该发送不同的信息,因为它是一本字典,每个按钮都是一个词。但我想通过一个发件人来完成。我尝试了以下代码来制作它,但它不起作用。

p.s.: 我尝试制作一个数组,但 Xcode 变得很疯狂。

@IBAction func abstractionENG(sender:UIButton) {
  return perfomanceWithIdentifier("menuENG",sender:nil)
}

我想你可以把字典发过来,这行有问题return perfomanceWithIdentifier("menuENG",sender:nil)

无论如何,您可以单独识别 tag 单击了哪个按钮,并根据单击的按钮创建字典,现在您可以将完整的字典发送给发件人。

@IBAction func abstractionENG(sender:UIButton) {

        var dictSendData:[String:Any] = [:]
        if sender == btn1
        {
            dictSendData.updateValue("abc", forKey: "key1")
            dictSendData.updateValue("pqr", forKey: "key2")
        }
        else if sender == btn2
        {
            dictSendData.updateValue("xyz", forKey: "key1")
            dictSendData.updateValue("123", forKey: "key2")

        }
       else 
        {
           dictSendData.updateValue("123", forKey: "key1")
           dictSendData.updateValue("abc", forKey: "key2")
        }
 self.performSegue(withIdentifier:"menuENG", sender: dictSendData)

}

1- 将 segue 动作分配给 IBAction 函数 - 将标签 ID 分配给所有按钮

你已经实现了。

2- 你的 IBAction 函数应该 运行 另一个函数到 运行 performSegue function

示例:

self.performSegue(withIdentifier: "openAnotherViewController",sender: sender)

3- 转到目标视图控制器并创建接收器变量 "Maybe you can use optionals " .

 var receiverInt :Int = 0
 var receiverInt2 :Int = 0

3-转到源视图控制器并传递变量

     // MARK: - Navigation
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 // Get the new view controller using segue.destinationViewController.
 // Pass the selected variable/object to the new view controller .

    if segue.identifier == "openAnotherViewController" {
        let destinationController =  segue.destination as! OtherViewControllerClass Name 
       // identify button by tag number 
       if (sender as! UIButton).tag == 200 {
        destinationController.receiverInt = self.sourceInt

        }else{
        destinationController.receiverInt2 = self.sourceInt2}

    }


 }

感谢大家通过这种方式帮助解决问题>

class MainViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

/* The following functions containts a group of conditionals which will change the scene depending  of the IBAction selected */

@IBAction func abstractionENG(sender: UIButton) {
    let data = [DiccioModel.abstraction().nameEng,DiccioModel.abstraction().descriptionEng,DiccioModel.abstraction().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}

@IBAction func webBrowserENG(sender: UIButton) {
    let data = [DiccioModel.webBrowser().nameEng,DiccioModel.webBrowser().descriptionEng,DiccioModel.webBrowser().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}

@IBAction func latencyENG(sender: UIButton) {
    let data = [DiccioModel.latency().nameEng,DiccioModel.latency().descriptionEng,DiccioModel.latency().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}

@IBAction func conditionalENG(sender: UIButton) {
    let data = [DiccioModel.conditional().nameEng,DiccioModel.conditional().descriptionEng,DiccioModel.conditional().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}


@IBAction func operatingSystemENG(sender: UIButton) {
    let data = [DiccioModel.os().nameEng,DiccioModel.os().descriptionEng,DiccioModel.os().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}

@IBAction func abstractionESP(sender: UIButton) {
    let data = [DiccioModel.abstraction().nameEsp,DiccioModel.abstraction().descriptionEsp,DiccioModel.abstraction().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}

@IBAction func webBrowserESP(sender: UIButton) {
    let data = [DiccioModel.webBrowser().nameEsp,DiccioModel.webBrowser().descriptionEsp,DiccioModel.webBrowser().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}

@IBAction func latencyESP(sender: UIButton) {
    let data = [DiccioModel.latency().nameEsp,DiccioModel.latency().descriptionEsp,DiccioModel.latency().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}

@IBAction func conditionalESP(sender: UIButton) {
    let data = [DiccioModel.conditional().nameEsp,DiccioModel.conditional().descriptionEsp,DiccioModel.conditional().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}


@IBAction func operatingSystemESP(sender: UIButton) {
    let data = [DiccioModel.os().nameEsp,DiccioModel.os().descriptionEsp,DiccioModel.os().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "menuENG") || (segue.identifier == "menuESP"){
        if let destinationViewController = segue.destinationViewController as? DefinitionViewController{
            if let data = sender as? Array<String>{
                destinationViewController.tittle = data[0]
                destinationViewController.def = data[1]
                destinationViewController.link = data[2]
            }
        }
    }



}

PS:请注意此代码已连接到 DefinitionViewController(视图的控制器)和模型。 (项目是通过 M.V.C 方式制作的)。

再次感谢大家的帮助。希望代码对以后的其他人有所帮助。