我有两段几乎完全相同的代码。一个用于popover视图控制器collectionView,它非常有用。我为"普通香草"改编了相同的代码。视图控制器(嵌入在导航控制器中),但我无法使用let delegate = self.delegate设置委托。它总是返回" nil"。我很确定我的故事板和#34;布线"设置正确(数据源,委托等)。
查看了大部分答案,我似乎无法找到有效的答案。谢谢!
这是无效的代码。我在这里尝试选择一个单元格并转到另一个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的对象
import UIKit
class MainImageViewController: UIViewController, FileCollectionViewControllerDelegate {
var fileName: String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//delegate methods
func fileSelected(selectedFile: String) {
print("got delegate selectedFile ",selectedFile)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "goToMainImage"{
let navController = segue.destinationViewController as? FileCollectionViewController
if let vc = navController {
vc.delegate = self
}
}
}
答案 0 :(得分:0)
感谢截图,帮助弄清楚可能出错的地方。您在故事板中设置的内容是delegate
的{{1}}和datasource
。这看起来是正确的。
在UICollectionView
方法中,您正在检查您在viewController中定义的didSelectItemAtIndexPath
属性,并且您发布的代码似乎并未将其设置在任何位置。
您需要将delegate
设置为实现协议的类的对象。由于代码没有显示任何实现协议的对象,我不知道该对象是什么,所以我为您提供了更具体的答案。
如果您需要更多帮助,请分享更多关于这些缺失的内容,并尝试为您提供更多帮助。让我知道它是怎么回事。