2017-04-17 85 views
0

我看到帖子的情侶,但沒能找出問題問題從UicollectionView控制器

我想從UICollectionView控制器的數據傳遞給我的UIViewController傳遞數據,下面是詳細信息

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "Selection2" { 

     if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell) { 

      let detailVC = segue.destination as! SelectionViewController 
      // 
      let item = items1[indexPath.row] 

      //passing the item name which is selected 
      detailVC.label1.text = item.name1 


     } 
    } 
} 

這裏是SelectionViewController代碼:

import UIKit 

class SelctionViewController: UIViewController { 

var label1 : UILabel! 


@IBOutlet weak var Label: UILabel! 

override func viewDidLoad() { 
    super.viewDidLoad() 


    Label.text = self.label1.text 



    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

} 

但是,當我運行這個我不能夠傳遞價值和得到以下錯誤:

致命錯誤:意外發現零而展開的可選值

請建議如何得以糾正,謝謝

+0

我會說,或者'sender'不是'UICollectionViewCell'或'segue.destination'不是'SelectionViewController'。你可以添加一個異常斷點來查看應用程序崩潰的位置嗎? –

回答

1

我會建議您使用以下方法。在didSelectItemAt方法中寫下面的代碼。

let detailVC = self.storyboard?.instantiateViewController(withIdentifier: "Selection2") as! SelectionViewController 
let item = items1[indexPath.row] 

//Create string property itemName and pass the value 
detailVC.itemName = item.name1 

self.navigationController?.pushViewController(detailVC, animated: true) 

在SelectionViewController中像下面一樣將itemName賦值爲laber。

self.Label.text = ITEMNAME

+0

我試過你的建議,並做了以下更改:var itemName:String!然後在代碼self.Label.text = itemName中調用像這樣的值,它不會崩潰,但不會在標籤視圖中顯示任何內容,我檢查並將它正確連接爲出口 –

+0

任何洞察爲什麼值不顯示? –

+0

@GS你可以在didSelectItemAt方法中加入斷點,並檢查item.name1是否有值。 – lazyCoder

0

而不是使用的UILabel LABEL1,聲明字符串變量如。標題。並在代碼中更改以下行。

類SelctionViewController:UIViewController的{

var title = "" 

@IBOutlet weak var Label: UILabel! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    Label.text = title 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

prepareForSegue方法:代替detailVC.label1.text,使用detailVC.title

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
if segue.identifier == "Selection2" { 

    if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell) { 

     let detailVC = segue.destination as! SelectionViewController 
     let item = items1[indexPath.row] 

     detailVC.title = item.name1 // instead of detailVC.label1.text, use detailVC.title 

    } 
} 
} 
+0

如果定義上述方法會給出很多錯誤 –

+0

它對我來說工作正常,但我已根據您的代碼進行了編輯。 –

0

@lazyCoder,感謝幫助我走出

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let detailVC = self.storyboard?.instantiateViewController(withIdentifier: "Selection2") as! SelctionViewController 

    detailVC.itemName = self.items1[indexPath.row].name1 


self.navigationController?.pushViewController(detailVC, animated: true) 

} 

現在這個代碼工作完全正常..Cheers