我有兩個幾乎完全相同的代碼。一個是爲popover視圖控制器collectionView,它工作得很好。我已經爲「普通香草」視圖控制器(嵌入在導航控制器中)調整了相同的代碼,但我無法使用let delegate = self.delegate設置委託。它總是返回「無」。很確定我有故事板「佈線」設置正確(數據源,委託等)。繼續獲取委託=無UICollectionView
通過大部分的答案看,我似乎無法找到一個工程。謝謝!
以下是無法使用的代碼。我想在這裏選擇一個單元格,並繼續到另一個viewController。
protocol FileCollectionViewControllerDelegate {
func fileSelected(controller: FileCollectionViewController, selectedFile: String)
}
class FileCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var links : [String] = ["Black", "Dark Gray", "Light Gray", "Blue","Purple", "Green", "Orange","Red" ]
var delegate: FileCollectionViewControllerDelegate?
@IBOutlet weak var fileCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
self.fileCollectionView.backgroundColor = UIColor.whiteColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
}
func numberOfSectionsInCollectionView(fileCollectionView: UICollectionView) -> Int {
return 1
}
func collectionView(fileCollectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return links.count
}
func collectionView(fileCollectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = fileCollectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! FileCollectionViewCell
cell.backgroundColor = UIColor.lightGrayColor()
cell.fileTitle.text = links[indexPath.row]
return cell
}
func collectionView(fileCollectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("delegate", self.delegate)
if let delegate = self.delegate { //HERE THE delegate is always nil ???
print("go to didSelect")
delegate.fileSelected(self, selectedFile: "silly")
self.performSegueWithIdentifier("goToMainImage", sender: self)
self.dismissViewControllerAnimated(true, completion: nil)
}
}
}
謝謝。你是最棒的。
對象實現FileCollectionViewControllerDelegate
的截圖
看來你可能會錯誤使用viewController的委託變量collectionView委託。你在故事板中設置了哪一個? –
卡塔利娜,我不確定我是否理解,但我在故事板中添加了連接的屏幕截圖。我可以通過編程方式設置正確的連接嗎? –