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

问题描述:

我试图发送多个变量在一个单一的发件人显示它在Viewcontroller它连接到名为menuENG的segue。我有五个按钮,每个按钮应发送不同的信息,因为是一本字典,每个按钮都是一个字。但我想通过一个发件人这样做。我尝试了下面的代码,但它不起作用。如何在一个发件人中发送多个变量?

p.s .:我尝试了一个数组,但Xcode疯了。

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

您不能在IBAction函数中返回。 –

+0

你的意思是说,所有五个按钮都连接到abstractionENG函数? –

+0

你真的想要什么?你能解释一下吗? –

谢谢大家的帮助通过这种方式>

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方式完成)。

再次Thx大家为你的帮助。希望代码将来可以帮助其他人。

我想你可以把字典,有什么不对的这条线return perfomanceWithIdentifier("menuENG",sender:nil)

反正你可以seperatly确定由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) 

} 
+0

标签是邪恶的。直接比较'sender',例如'如果sender == button1'更好。或者为每个按钮声明一个特殊功能。不要推荐标签。他们只是魔术数字。 – Sulthan

+0

@Sulthan我认为标签是有用的,当我们还没有创建所有按钮的出口 –

+0

正好,例如http://benford.me/blog/12-dos-and-donts-for-ios-development/说*“的标签属性可能被称为“lazyHackUsedHere”“*,这是完全正确的。他们只是不好的代码。 – Sulthan

1-分配SEGUE行动IBAction为功能 - 分配标签ID永远按钮

你已经实现了它。

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

例如:

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

3-去到目的地视图控制器,并创建reciver变量“也许你可以使用选配”。

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} 

    } 


} 
+0

我想你忘了阅读'按钮应该发送不同的信息,因为是一本字典,每个按钮是一个字' –

+0

我已经更新了我的答案,告诉他如何使用标签。 他不知道如何用户segue这就是为什么我发布示例 –