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

时间:2022-11-28 15:12:19

I'm trying to send multiple variables in one single sender to show it in the Viewcontroller that it's connected to a segue named menuENG. I have five buttons and each button should send different information because is a dictionary and each button is a word. But I want to do this thru one sender. I tried the following code to make it but it is not working.

我正在尝试在一个发送器中发送多个变量,以在Viewcontroller中显示它连接到名为menuENG的segue。我有五个按钮,每个按钮应该发送不同的信息,因为它是一个字典,每个按钮都是一个单词。但我想通过一个发件人这样做。我尝试了以下代码来实现它,但它无法正常工作。

p.s.: I tried making an array but Xcode goes crazy.

p.s。:我试过制作一个数组,但Xcode发疯了。

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

3 个解决方案

#1


1  

I think you can send the dictionary and there is something wrong with this line return perfomanceWithIdentifier("menuENG",sender:nil)

我想你可以发送字典,这行返回perfomanceWithIdentifier(“menuENG”,sender:nil)有问题

Anyway you can seperatly identify which button is clicked by tag and create dictionary on base of clicked button now you can send the complete dictionary to the sender.

无论如何,您可以单独识别按标签单击的按钮,并在单击按钮的基础上创建字典,现在您可以将完整的字典发送给发件人。

@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)

}

#2


0  

1- assign segue action to IBAction function - assign tag id to ever button

1-将segue动作分配给IBAction功能 - 将标签ID分配给ever按钮

you have already implemented it .

你已经实现了它。

2- your IBAction function should run another function to run performSegue function

2-你的IBAction函数应运行另一个函数来运行performSegue函数

example :

例子:

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

3- go to destination view Controller and create reciver variables "Maybe you can use optionals " .

3-转到目标视图控制器并创建reciver变量“也许你可以使用选项”。

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

3- go to the source view Controller and pass variables

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}

    }


 }

#3


0  

Thank you everyone for the help fixed the issue by this way >

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

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: note this code is connected to the DefinitionViewController ( Controller of the view) and to a model. ( project was made by M.V.C way).

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

Again Thx everyone for your help. hope the code help other people in the future.

再次感谢大家的帮助。希望代码在未来帮助其他人。

#1


1  

I think you can send the dictionary and there is something wrong with this line return perfomanceWithIdentifier("menuENG",sender:nil)

我想你可以发送字典,这行返回perfomanceWithIdentifier(“menuENG”,sender:nil)有问题

Anyway you can seperatly identify which button is clicked by tag and create dictionary on base of clicked button now you can send the complete dictionary to the sender.

无论如何,您可以单独识别按标签单击的按钮,并在单击按钮的基础上创建字典,现在您可以将完整的字典发送给发件人。

@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)

}

#2


0  

1- assign segue action to IBAction function - assign tag id to ever button

1-将segue动作分配给IBAction功能 - 将标签ID分配给ever按钮

you have already implemented it .

你已经实现了它。

2- your IBAction function should run another function to run performSegue function

2-你的IBAction函数应运行另一个函数来运行performSegue函数

example :

例子:

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

3- go to destination view Controller and create reciver variables "Maybe you can use optionals " .

3-转到目标视图控制器并创建reciver变量“也许你可以使用选项”。

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

3- go to the source view Controller and pass variables

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}

    }


 }

#3


0  

Thank you everyone for the help fixed the issue by this way >

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

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: note this code is connected to the DefinitionViewController ( Controller of the view) and to a model. ( project was made by M.V.C way).

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

Again Thx everyone for your help. hope the code help other people in the future.

再次感谢大家的帮助。希望代码在未来帮助其他人。