From 482fb7deb9a4edb7877ef95f606e868faa9191b6 Mon Sep 17 00:00:00 2001 From: Simon de Carufel Date: Mon, 28 Dec 2015 14:50:55 -0500 Subject: [PATCH] Change pager strip viewControllers for titles Change pager strip's private viewController array for a title array (of NSString object). This change make abstraction of the pagerController implementation, and allows pagerStrip to be used without requiring UIViewControllers. --- .../xcschemes/MRGPagerController.xcscheme | 10 ++++---- Pod/Classes/MRGPagerController.m | 19 +++++++++++---- Pod/Classes/MRGPagerStrip.h | 6 ++--- Pod/Classes/MRGPagerTabStrip.m | 11 ++++----- Pod/Classes/MRGPagerTitleStrip.m | 23 ++++++++----------- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/MRGPagerController.xcscheme b/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/MRGPagerController.xcscheme index b449dce..5a06a9a 100644 --- a/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/MRGPagerController.xcscheme +++ b/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/MRGPagerController.xcscheme @@ -13,11 +13,11 @@ buildForProfiling = "YES" buildForArchiving = "YES"> + BuildableIdentifier = "primary" + BlueprintIdentifier = "66F1C21F1E93A364A7427FB739D1D1A7" + BlueprintName = "MRGPagerController" + ReferencedContainer = "container:Pods.xcodeproj" + BuildableName = "libMRGPagerController.a"> diff --git a/Pod/Classes/MRGPagerController.m b/Pod/Classes/MRGPagerController.m index abb7420..3b03627 100644 --- a/Pod/Classes/MRGPagerController.m +++ b/Pod/Classes/MRGPagerController.m @@ -72,7 +72,7 @@ - (void)viewDidLoad { if (self.pagerStrip) { [self.view addSubview:self.pagerStrip]; self.pagerStrip.delegate = self; - self.pagerStrip.viewControllers = self.viewControllers; + self.pagerStrip.pageTitles = [self getPageTitles]; [self.view addSubview:self.pagerStrip]; } @@ -299,8 +299,8 @@ - (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated { if (_viewControllers != viewControllers) { NSArray *oldViewControllers = self.viewControllers; _viewControllers = [viewControllers copy]; - - [self.pagerStrip setViewControllers:self.viewControllers animated:animated]; + + [self.pagerStrip setPageTitles:[self getPageTitles] animated:animated]; [self updateViewControllersWithOldViewControllers:oldViewControllers newViewControllers:self.viewControllers animated:animated]; if ((self.pagerScrollView != nil)) { @@ -339,14 +339,23 @@ - (void)setPadding:(UIEdgeInsets)padding { } } +- (NSArray *)getPageTitles { + NSMutableArray *titles = [NSMutableArray arrayWithCapacity:self.viewControllers.count]; + for (UIViewController *viewController in self.viewControllers) { + [titles addObject:viewController.title]; + } + return titles; +} + #pragma mark - MRGPagerStripDelegate - (void)pagerStripSizeChanged:(id)pagerStrip { [self.view setNeedsLayout]; } -- (void)pagerStrip:(id)pagerStrip didSelectViewController:(UIViewController *)viewController { - [self setCurrentViewController:viewController animated:YES]; +- (void)pagerStrip:(id )pagerStrip didSelectPageAtIndex:(NSInteger)pageIndex +{ + [self setCurrentViewController:self.viewControllers[pageIndex] animated:YES]; } @end diff --git a/Pod/Classes/MRGPagerStrip.h b/Pod/Classes/MRGPagerStrip.h index 2fbeb8c..b2426bf 100644 --- a/Pod/Classes/MRGPagerStrip.h +++ b/Pod/Classes/MRGPagerStrip.h @@ -34,8 +34,8 @@ @property (nonatomic, weak) id delegate; -@property (nonatomic, copy) NSArray *viewControllers; -- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated; +@property (nonatomic, copy) NSArray *pageTitles; +- (void)setPageTitles:(NSArray *)pageTitles animated:(BOOL)animated; @property (nonatomic) CGFloat currentIndex; - (void)setCurrentIndex:(CGFloat)currentIndex animated:(BOOL)animated; @@ -45,6 +45,6 @@ @protocol MRGPagerStripDelegate - (void)pagerStripSizeChanged:(id)pagerStrip; -- (void)pagerStrip:(id)pagerStrip didSelectViewController:(UIViewController *)viewController; +- (void)pagerStrip:(id )pagerStrip didSelectPageAtIndex:(NSUInteger)pageIndex; @end diff --git a/Pod/Classes/MRGPagerTabStrip.m b/Pod/Classes/MRGPagerTabStrip.m index da73927..ca5caf9 100644 --- a/Pod/Classes/MRGPagerTabStrip.m +++ b/Pod/Classes/MRGPagerTabStrip.m @@ -34,8 +34,7 @@ @interface MRGPagerTabStrip () @implementation MRGPagerTabStrip -- (instancetype)initWithFrame:(CGRect)frame -{ +- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSParameterAssert(self.scrollView.delegate == nil); @@ -110,8 +109,8 @@ - (void)layoutSubviews { #pragma mark - get/set -- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated { - [super setViewControllers:viewControllers animated:animated]; +- (void)setPageTitles:(NSArray *)pageTitles animated:(BOOL)animated { + [super setPageTitles:pageTitles animated:animated]; [self.buttons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger index, BOOL *stop) { [button setTag:index]; @@ -133,8 +132,8 @@ - (UIImage *)backgroundImageWithColor:(UIColor *)color { - (void)buttonTapped:(UIButton *)button { NSUInteger index = button.tag; - if (index < self.viewControllers.count) { - [self.delegate pagerStrip:self didSelectViewController:[self.viewControllers objectAtIndex:index]]; + if (index < self.pageTitles.count) { + [self.delegate pagerStrip:self didSelectPageAtIndex:index]; } } diff --git a/Pod/Classes/MRGPagerTitleStrip.m b/Pod/Classes/MRGPagerTitleStrip.m index 42c6d35..47f7ba1 100644 --- a/Pod/Classes/MRGPagerTitleStrip.m +++ b/Pod/Classes/MRGPagerTitleStrip.m @@ -38,12 +38,11 @@ @interface MRGPagerTitleStrip () @implementation MRGPagerTitleStrip -@synthesize viewControllers = _viewControllers; +@synthesize pageTitles = _pageTitles; @synthesize currentIndex = _currentIndex; @synthesize delegate = _delegate; -- (instancetype)initWithFrame:(CGRect)frame -{ +- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor blackColor]; @@ -182,9 +181,9 @@ - (void)updateButtonsAnimated:(BOOL)animated { [self.buttons makeObjectsPerformSelector:@selector(removeFromSuperview)]; [self.buttons removeAllObjects]; - for (UIViewController *viewController in self.viewControllers) { + for (NSString *title in self.pageTitles) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; - [button setTitle:viewController.title forState:UIControlStateNormal]; + [button setTitle:title forState:UIControlStateNormal]; [self.scrollView addSubview:button]; [self.buttons addObject:button]; } @@ -238,17 +237,17 @@ - (void)updateButtonIndex:(CGFloat)index { #pragma mark - get/set -- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated { - if (_viewControllers != viewControllers) { - _viewControllers = [viewControllers copy]; +- (void)setPageTitles:(NSArray *)pageTitles animated:(BOOL)animated { + if (_pageTitles != pageTitles) { + _pageTitles = [pageTitles copy]; [self updateButtonsAnimated:animated]; [self setNeedsUpdateView]; } } -- (void)setViewControllers:(NSArray *)viewControllers { - [self setViewControllers:viewControllers animated:NO]; +- (void)setPageTitles:(NSArray *)pageTitles { + [self setPageTitles:pageTitles animated:NO]; } - (void)setCurrentIndex:(CGFloat)currentIndex animated:(BOOL)animated { @@ -259,10 +258,6 @@ - (void)setCurrentIndex:(CGFloat)currentIndex animated:(BOOL)animated { } } -- (void)setCurrentViewControllerIndex:(CGFloat)index { - [self setCurrentIndex:index animated:NO]; -} - - (void)setTitleTextAlignment:(NSTextAlignment)titleTextAlignment { if (_titleTextAlignment != titleTextAlignment) { _titleTextAlignment = titleTextAlignment;