2017-08-28 16 views
0

之間的數據我有兩個VC的,我試圖將數據從第一個VC傳遞到第二個使用segue。第一個VC有一個tableview,每個單元格包含一個標題和一個使用核心數據存儲的描述標籤。我想在第二個VC中有每個單元的詳細視圖。我已初始化兩個字符串來存儲來自第一個VC的值,並將它們分配給第二個VC中的字符串。無法通過VC

請看看下面的代碼:

var SelectedTitle = String() 
var SelectedDisc = String() 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    SelectedTitle = self.listAthkar[indexPath.row].title! 
    print(SelectedTitle) 
    SelectedDisc = self.listAthkar[indexPath.row].details! 
    print(SelectedDisc) 
    performSegue(withIdentifier: "showCell", sender: self) 
} 


override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if (segue.identifier == "showCell") { 
     var destinationVC = segue.destination as! showCellVC 
     destinationVC.passedTittle = SelectedTitle 
     destinationVC.passedDisc = SelectedDisc 
    } 
} 


class showCellVC: UIViewController { 

var passedTittle = String() 
var passedDisc = String() 
@IBOutlet weak var cellTitle: UILabel! 
@IBOutlet weak var cellDisc: UITextView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    cellTitle.text = passedTittle 
    cellDisc.text = passedDisc 
    print("Title Passed is \(passedTittle)") 
    print("Discription Passed is \(passedDisc)") 
} 

當我測試應用程序控制臺打印:

Title Passed is 
Discription Passed is 
title 
Description 

下面是實際的細胞的照片

Cell picture 任何想法爲什麼它沒有將數據傳遞給第二個VC?

+0

一些建議,但首先,你的segue代碼與我的有點不同,但它*看起來像它應該工作。 (1)將您的打印語句移入'prepare(for sequels:sender:)'並確保*第一個VC變量是正確的。 (2)向我們展示 - 或者至少檢查一下自己 - 如果在IB中正確設置了賽會,打字錯誤或者可能存在問題。 (3)另一個檢查是確保你正確地實例化第二個VC - 考慮將你的代碼改爲'if let destinationVC = segue.destination as? showCellVC'並在那裏傳遞變量if。 – dfd

+0

請使用Swift命名約定,該命名約定是變量名稱的低位字節。 –

+0

print(SelectedTitle)在didSelectetRowAt中有一個require值嗎? –

回答

3

獨立陣列看起來你已經在你的故事板把一個action賽格瑞您的tableview細胞。既然你正在使用程序化的segue,你不需要這個。在您的didSelect方法之前,動作繼續發生,因此在設置變量之前發生了繼續。

+0

非常感謝。我從單元格中刪除了segue,並將其放入視圖控制器中並工作。 –

0

請嘗試這樣!作出「標題」和「說明」

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    self.performSegue(withIdentifier: "showCell", sender: indexPath) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if (segue.identifier == "showCell") { 
    var destinationVC = segue.destination as! showCellVC 
    destinationVC.passedTittle = self.listAthkar[(sender as! IndexPath).row] 
    destinationVC.passedDisc = self.listAthkar[(sender as! IndexPath).row] 
    } 
}