UIScrollView 实现委托
我有一个视图控制器,它具有三个滚动视图作为子视图。我想让子滚动视图在页面发生更改时通知父视图控制器。
我想我可以通过设置委托模式来做到这一点。我的孩子( 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能不明白整个问题,但我没有看到实现您自己的 UIScrollView 子类的原因,如果您坚持这样做,请不要隐藏它的 .delegate 属性或确保并调用 super。
我将通过执行以下操作来解决此问题:
假设所有这些滚动视图都包含在其他视图中;
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;