问题描述
我有UIScrollView的子类,我需要在内部对滚动行为做出响应.但是,viewcontroller仍将需要侦听滚动的委托回调,因此我无法彻底窃取组件中的委托.
I have a subclass of UIScrollView in which I need to internally respond to scrolling behaviour. However, the viewcontroller will still need to listen to scrolling delegate callbacks, so I can't outright steal the delegate within my component.
是否有一种方法可以保留名为"delegate"的属性并仅侦听沿其发送的消息,或者以某种方式在内部内部劫持委托属性并在运行一些代码后向外转发消息?
Is there a way to keep the property named "delegate" and just listen to messages sent along it, or else somehow internally hijack the delegate property and forward messages outward after running some code?
推荐答案
是的,但是您必须覆盖文档.基本上,创建第二个委托属性并实现委托协议.调用委托方法时,要照顾好您的业务,然后从刚刚运行的委托方法中在第二个委托上调用相同的方法.例如.
Yes, but you'll have to override every delegate method in the docs. Basically, make a second delegate property and implement the delegate protocol. When your delegate methods are called, take care of your business and then call the same method on your second delegate from the delegate method that was just run. E.g.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Do stuff here
if ([self.delegate2 respondsToSelector:@selector(scrollViewDidScroll:)]) {
[self.delegate2 scrollViewDidScroll:scrollView];
}
}
这篇关于拦截子类中的Objective-C委托消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!