- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我对 NSCollectionViewDelegate
进行了扩展,在其中我声明了两个新函数来处理 NSCollectionViewItems 上的点击。我从自定义 NSCollectionViewItems 子类调用委托(delegate)方法,并在我的 ViewController 中将 collectionView 的委托(delegate)设置为 self
。然而,新函数是在 NSCollectionViewDelegate
中调用的,而不是在我的 ViewController 中调用的。
我的 NSCollectionViewDelegate 扩展:
extension NSCollectionViewDelegate {
func collectionView(_ collectionView: NSCollectionView, didDoubleClickOnItem item: Int) {
print("didDoubleClickOnItem delegate")
}
func collectionView(_ collectionView: NSCollectionView, didRightClickOnItem item: Int) {
print("didRightClickOnItem delegate")
}
}
这些函数在我的 NSCollectionViewItem 子类中调用:
self.collectionView.delegate?.collectionView(self.collectionView, didDoubleClickOnItem: itemIndex)
但是在我的ViewController中实现的功能
func collectionView(_ collectionView: NSCollectionView, didDoubleClickOnItem item: Int) {
print("didDoubleClickOnItem")
}
不被调用。
我做错了什么?
提前致谢,费边
最佳答案
就像@AMAN77 所说的那样,您已经使用一些新的(并且已经实现的)功能扩展了 NSCollectionViewDelegate
的基本委托(delegate)功能,因此编译器认为这没有什么坏处。
没有规则强制您只能使用一个委托(delegate)。如果您希望调用您的 ViewController
,作为委托(delegate) NSCollectionViewItems
的委托(delegate),请执行以下操作。
创建满足您需求的协议(protocol);有人会告诉你的委托(delegate)什么?
大概是这样的:
protocol NSCollectionViewClickHandler {
func collectionView(_ collectionView: NSCollectionView, didDoubleClickOnItem item: Int)
func collectionView(_ collectionView: NSCollectionView, didRightClickOnItem item: Int)
}
因此,既然您希望 ViewController
充当委托(delegate),它就应该实现这些功能。 (您至少已经这样做了 didDoubleClickOnItem
)
恕我直言,如果您在 ViewController
的扩展中这样做,看起来很不错
class ViewController {
// All the regular stuff goes in here
// …
override func viewDidLoad() {
super.viewDidLoad()
// set self as (NSCollectionViewClickHandler) delegate of some object
}
}
extension ViewController: NSCollectionViewDelegate {
// implement those function of NSCollectionViewDelegate that you'd like to use,
// and the ones that are required.
}
extension ViewController: NSCollectionViewClickHandler {
func collectionView(_ collectionView: NSCollectionView, didDoubleClickOnItem item: Int) {
print("didDoubleClickOnItem")
}
func collectionView(_ collectionView: NSCollectionView, didRightClickOnItem item: Int) {
print("didDoubleClickOnItem")
}
// If you need other methods to properly implement your delegate methods,
// you can group them in this extension as well: they logically belong together.
// …
}
class YourSubclassOfNSCollectionViewItems: NSCollectionViewItems {
var clickDelegate: NSCollectionViewClickHandler?
func someFunction() {
// does something, and gets the itemIndex
clickDelegate?.collectionView(self.collectionView, didDoubleClickOnItem: itemIndex)
}
}
我希望你现在对如何使用 proto 有了更好的理解-…呃我的意思是delegates ;-)祝你好运! (奖金提示:here is a quick-and-easy intro to learn more about delegates and other design patterns)
关于swift macOS - 自定义 NSCollectionViewDelegate 不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574217/
我有一个子类NSCollectionView它在 Interface Builder 中创建并绑定(bind)到一个数组。 我需要实现 NSCollectionViewDelegate 中的一些委托(
我对 NSCollectionViewDelegate 进行了扩展,在其中我声明了两个新函数来处理 NSCollectionViewItems 上的点击。我从自定义 NSCollectionViewI
我是一名优秀的程序员,十分优秀!