2017-08-12 46 views
0

我試圖發送多個變量在一個單一的發件人顯示它在Viewcontroller它連接到名爲menuENG的segue。我有五個按鈕,每個按鈕應發送不同的信息,因爲是一本字典,每個按鈕都是一個字。但我想通過一個發件人這樣做。我嘗試了下面的代碼,但它不起作用。如何在一個發件人中發送多個變量?

p.s .:我嘗試了一個數組,但Xcode瘋了。

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

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

+0

你的意思是說,所有五個按鈕都連接到abstractionENG函數? –

+0

你真的想要什麼?你能解釋一下嗎? –

回答

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大家爲你的幫助。希望代碼將來可以幫助其他人。

1

我想你可以把字典,有什麼不對的這條線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

0

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這就是爲什麼我發佈示例 –