每个对象有多个委托? [英] Multiple delegates per one object?
问题描述
我有一个 UIScrollView
,我需要子类,在子类中,我需要附加 UIScrollViewDelegate
可以实现 viewForZoomingInScrollView
方法。然后我有一个 UIViewController
其中我需要实例化这个 UIScrollView的对象
我创建的子类,我也想让UIViewController一个
UIScrollViewDelegate
这个对象,所以我可以实现 scrollViewDidZoom
在此 UIViewController
类中。 如何使一个对象有两个代理? (我知道我可以很容易地只有一个委托,只是实现两个方法,但在设计目的,我想做的方式,我提到)。
您不需要具有2个代表的对象。你想让你的customScrollView保持自己的UIScrollViewDelegate函数的责任。
为了让你的parentVC响应UIScrollView的委托方法,你必须做一个自定义
现在,当UIScrollViewDelegate函数被调用时,你还将从你的自定义委托中调用你的一个委托函数。这样,您的parentVC将在您想要的时刻做出响应。
它看起来会像这样。
CustomScrollView.h
@protocol CustomDelegate< NSObject>
//自定义委托方法
- (void)myCustomDelegateMethod;
@end
@interface CustomScrollView:UIScrollView< UIScrollViewDelegate>
{
id< CustomDelegate>委托
//剩下的东西
CustomScrollView.m
- (void)viewForZoomingInScrollView
{
[self.delegate myCustomDelegateMethod];
//其余视图ForZoomingInScrollView代码
ParentVC.h
@interface CustomScrollView:UIViewController< CustomDelegate>
{
// stuff
ParentVC.m
- (void)makeCustomScrollView
{
CustomScrollView * csv = [[CustomScrollView alloc] init];
csv.delegate = self;
//其他东西
}
- (void)myCustomDelegateMethod
{
//响应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.
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).
You don't want an object with 2 delegates. You want to keep your customScrollView keep the responsibility of its own UIScrollViewDelegate functions.
To make your parentVC respond to the delegate methods of UIScrollView as well you will have to make a custom delegate inside your customScrollView.
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.
It will look somewhat like this.
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
-(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.
这篇关于每个对象有多个委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!