我实现了一个类别来为我处理这个问题,它让我的代码变得一团糟,并允许我通过“pageController.pageControl”访问 pageControl
Objective-C
// Header
@interface UIPageViewController (PageControl)
@property (nonatomic, readonly) UIPageControl *pageControl;
@end
我还使用了递归(由块处理),以防 Apple 决定更改实现导致 UIPageControl 不在子视图的第一层中。
// Implementation
#import "UIPageViewController+PageControl.h"
@implementation UIPageViewController (PageControl)
- (UIPageControl *)pageControl
{
__block UIPageControl *pageControl = nil;
void (^pageControlAssignBlock)(UIPageControl *) = ^void(UIPageControl *blockPageControl) {
pageControl = blockPageControl;
};
[self recurseForPageControlFromSubViews:self.view.subviews withAssignBlock:pageControlAssignBlock];
return pageControl;
}
- (void)recurseForPageControlFromSubViews:(NSArray *)subViews withAssignBlock:(void (^)(UIPageControl *))assignBlock
{
for (UIView *subView in subViews) {
if ([subView isKindOfClass:[UIPageControl class]]) {
assignBlock((UIPageControl *)subView);
break;
} else {
[self recurseForPageControlFromSubViews:subView.subviews withAssignBlock:assignBlock];
}
}
}
@end
这可能对您的需求有点过分,但对我来说效果很好