Ich habe mehrere View-Controller, die die gleiche Art von Zellen zeigt. Ich möchte wie dies in einer Protokollerweiterung Delegierten setzen:So legen Sie Delegat in einer Protokollerweiterung fest
class ProductsViewController: UIViewController, ProductShowcase {
//other properties
@IBOutlet weak var productCollectionView: UICollectionView!
var dataSource: DataSource!
override func viewDidLoad() {
super.viewDidLoad()
setupDataSource()
setupCollectionView()
}
func didSelectProduct(product: Product) {
print(product)
}
//other functions
}
protocol ProductShowcase: UICollectionViewDelegate {
var dataSource: DataSource! { get set }
var productCollectionView: UICollectionView! { get }
func didSelectProduct(product: Product)
}
extension ProductShowcase {
func setupCollectionView() {
productCollectionView.registerClass(ProductCollectionViewCell.self, forCellWithReuseIdentifier: "productCell")
productCollectionView.dataSource = dataSource
print(self) //prints ProductsViewController
productCollectionView.delegate = self //
print(productCollectionView.delegate) //prints optional ProductsViewController
}
}
extension ProductShowcase {
//this delegate method is not called
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
didSelectProduct(dataSource.dataObjects[indexPath.row])
}
}
Wenn didSelectItemAtIndexPath
in ProductsViewController
implementiert wird es aufgerufen wird. Gibt es etwas, was ich vermisst habe oder ist das eine falsche Herangehensweise?
Ich glaube, Sie in die aktuelle Objective-C Interoperabilität Einschränkung ausgeführt werden hier beschrieben: http://stackoverflow.com/questions/39487168/non-objc -method-does-not-satisfy-optional-Anforderung-of-objc-protocol/39604189 # 39604189 –