UIScrollView 实现委托

发布于 2024-11-02 10:21:58 字数 960 浏览 3 评论 0原文

我有一个视图控制器,它具有三个滚动视图作为子视图。我想让子滚动视图在页面发生更改时通知父视图控制器。

我想我可以通过设置委托模式来做到这一点。我的孩子( UIScrollView 的子类)在头文件中设置它:

@interface TriptychScrollView : UIScrollView <UIScrollViewDelegate> {
    id delegate;
}

- (id)delegate;
- (void)setDelegate:(id)newDelegate;
@end

@protocol RCScrollDidChange
@optional
- (void)scrollView:(TriptychScrollView *)scrollView imageDidChange:(NSNumber *)index;
@end

源文件具有委托访问器方法:

- (id)delegate {
    return delegate;
}
- (void)setDelegate:(id)newDelegate {
    delegate = newDelegate;
}

不幸的是,这意味着我将委托设置为自我调用被忽略:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setDelegate:self];
    }
    return self;
}

..因此这两个方法 scrollViewDidScroll:scrollViewDidEndDecelerating: 没有被调用!这意味着我实际上失去了对滚动视图的控制。

我这样做错了吗?或者是否有更好的方法让子视图将消息发送回其父母?

I have a view controller that has three scroll views as subviews. I would like to have the child scrolling views inform the parent view controller whenever the page has changed.

I thought I would do this by setting up a delegation pattern. My child (a subclass of UIScrollView) sets it up in the header file:

@interface TriptychScrollView : UIScrollView <UIScrollViewDelegate> {
    id delegate;
}

- (id)delegate;
- (void)setDelegate:(id)newDelegate;
@end

@protocol RCScrollDidChange
@optional
- (void)scrollView:(TriptychScrollView *)scrollView imageDidChange:(NSNumber *)index;
@end

The source file has the delegate accessor methods:

- (id)delegate {
    return delegate;
}
- (void)setDelegate:(id)newDelegate {
    delegate = newDelegate;
}

Unfortunately, this means my setting the delegate to self call is being ignored:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setDelegate:self];
    }
    return self;
}

.. and therefore the two methods scrollViewDidScroll: and scrollViewDidEndDecelerating: are not getting called! This means I've effectively lost control of my scrolling views.

Am I doing this incorrectly? Or is there a better way for the child subviews to send messages back to their parents?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2024-11-09 10:21:58

我可能不明白整个问题,但我没有看到实现您自己的 UIScrollView 子类的原因,如果您坚持这样做,请不要隐藏它的 .delegate 属性或确保并调用 super。

我将通过执行以下操作来解决此问题:

假设所有这些滚动视图都包含在其他视图中;

UIScrollView *a = ....
a.delegate = self;
a.tag = 1;

UIScrollView *b = ....
b.delegate = self;
b.tag = 2;

UIScrollView *c = ....
c.delegate = self;
c.tag = 3

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   if (scrollView.tag == 1) 
     //handle a
   else if (scrollView.tag == 2)
     //handle b
   else if (scrollView.tag == 3)
     //handle c
}

I may not understand the whole problem, but I dont see a reason for implementing your own subclasses of UIScrollView, and if you are insistent on doing so, dont shadow it's .delegate property or make sure and call super.

I would approach this by doing the following:

assuming all of these scrollviews are contained by some other view;

UIScrollView *a = ....
a.delegate = self;
a.tag = 1;

UIScrollView *b = ....
b.delegate = self;
b.tag = 2;

UIScrollView *c = ....
c.delegate = self;
c.tag = 3

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   if (scrollView.tag == 1) 
     //handle a
   else if (scrollView.tag == 2)
     //handle b
   else if (scrollView.tag == 3)
     //handle c
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文