我有一个 UIScrollView
我需要子类化,并且在子类中我需要附加 UIScrollViewDelegate
以便我可以实现 viewForZoomingInScrollView
方法.
I have a UIScrollView
that I need to subclass and within the subclass I need to attach the UIScrollViewDelegate
so I can implement the viewForZoomingInScrollView
method.
然后我有一个 UIViewController
我需要在其中实例化我创建的这个 UIScrollView
子类的对象,并且我还想让 UIViewController 成为 UIScrollViewDelegate
用于这个对象,所以我可以在这个 UIViewController
类中实现 scrollViewDidZoom
.
Then I have a UIViewController
where I need to instantiate an object of this UIScrollView
subclass that I created, and I would also like to make the UIViewController a UIScrollViewDelegate
for this object so I can implement scrollViewDidZoom
in this UIViewController
class.
如何让一个对象有两个委托?(我知道我可以很容易地只拥有一个委托并在那里实现这两种方法,但出于设计目的,我想按照我提到的方式来做).
How is it possible to make one object have two delegates? (I know I could easily just have one delegate and just implement both methods there, but for design purposes I'd like to do it the way that I'm mentioning).
你不想要一个有 2 个委托的对象.您希望让您的 customScrollView 保持其自己的 UIScrollViewDelegate 函数的责任.
You don't want an object with 2 delegates. You want to keep your customScrollView keep the responsibility of its own UIScrollViewDelegate functions.
要让您的 parentVC 也响应 UIScrollView 的委托方法,您必须在您的 customScrollView 中创建一个自定义委托.
To make your parentVC respond to the delegate methods of UIScrollView as well you will have to make a custom delegate inside your customScrollView.
在调用 UIScrollViewDelegate 函数的那一刻,您还将从自定义委托中调用您的委托函数之一.这样,您的 parentVC 将在您希望的时候做出响应.
At the moment a UIScrollViewDelegate function gets called you will also call one of your delegate functions from your custom delegate. This way your parentVC will respond at the moment you want it to.
看起来有点像这样.
CustomScrollView.h
CustomScrollView.h
@protocol CustomDelegate <NSObject>
//custom delegate methods
-(void)myCustomDelegateMethod;
@end
@interface CustomScrollView : UIScrollView <UIScrollViewDelegate>
{
id<CustomDelegate> delegate
//the rest of the stuff
CustomScrollView.m
CustomScrollView.m
-(void) viewForZoomingInScrollView
{
[self.delegate myCustomDelegateMethod];
//rest of viewForZoomingInScrollView code
ParentVC.h
@interface CustomScrollView : UIViewController <CustomDelegate>
{
//stuff
ParentVC.m
-(void)makeCustomScrollView
{
CustomScrollView *csv = [[CustomScrollView alloc] init];
csv.delegate = self;
//other stuff
}
-(void)myCustomDelegateMethod
{
//respond to viewForZoomingInScrollView
}
我希望这可以完全解决您的问题.祝你好运.
I hope this fully covers your problem. Good luck.
这篇关于每个对象有多个代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!