Permalink
Browse files

Removed requirement to specify default button when the bar buttons ar…

…e being set up. Instead, selectViewController:childViewController:invokeSelector should be called to select the first viewController and execute the selector.
  • Loading branch information...
1 parent 6cc822c commit 3dadd29367e9b16df92665624039fc047f9379ef Gavin Conway committed Mar 17, 2012
Showing with 24 additions and 9 deletions.
  1. +2 −3 TPScrollBarController.h
  2. +22 −6 TPScrollBarController.m
View
@@ -42,12 +42,11 @@
@property(nonatomic, readonly) NSUInteger selectedScrollBarPage;
- (void)setBarButtons:(NSArray *)barButtons
- onScrollBarPages:(NSArray *)pageNumbers
- withDefaultButton:(UIButton *)defaultButton;
+ onScrollBarPages:(NSArray *)pageNumbers;
- (void)selectScrollBarPage:(NSUInteger)pageNumber animated:(BOOL)animated;
- (void)resizeScrollBarForNumberOfPages:(NSUInteger)pages;
-- (void)selectViewController:(UIViewController *)childViewController;
+- (void)selectViewController:(UIViewController *)childViewController invokeSelector:(SEL)selector;
- (void)setFullScreenMode:(BOOL)moveToFullScreen animated:(BOOL)animated;
// Subclass should override these methods to provide the correct view controller for the given button
View
@@ -29,6 +29,8 @@ @interface TPScrollBarController ()
@property(nonatomic, assign) BOOL shouldForwardAppearanceAndRotationMethodsToChildViewControllers;
- (void)performSelectorOnDelegate:(SEL)aSelector withObject:(id)param1 andObject:(id)param2;
+- (void)performSelector:(SEL)aSelector onViewController:(id)viewController;
+
- (void)initaliseContainerViews;
- (void)layoutBarButtons;
- (void)barButtonReceivedTouchDown:(UIButton *)sender;
@@ -132,7 +134,6 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
- (void)setBarButtons:(NSArray *)barButtons
onScrollBarPages:(NSArray *)pageNumbers
- withDefaultButton:(UIButton *)defaultButton
{
NSAssert([barButtons count] == [pageNumbers count], @"barButton and pageNumber arrays must contain the same number of objects");
for (UIButton *barButton in barButtons) {
@@ -148,7 +149,6 @@ - (void)setBarButtons:(NSArray *)barButtons
NSMutableArray *numbers = [NSMutableArray arrayWithArray:pageNumbers];
[numbers sortUsingDescriptors:[NSArray arrayWithObject:ascending]];
self.scrollBarPageSet = [NSOrderedSet orderedSetWithArray:numbers];
- self.selectedViewController = [self viewControllerForButton:defaultButton];
self.barButtons = barButtons;
self.scrollBarPageArray = pageNumbers;
@@ -172,11 +172,18 @@ - (void)selectScrollBarPage:(NSUInteger)pageNumber animated:(BOOL)animated
self.selectedScrollBarPage = pageNumber;
}
-- (void)selectViewController:(UIViewController *)childViewController
+- (void)selectViewController:(UIViewController *)childViewController invokeSelector:(SEL)selector
{
- [self transitionFromViewController:self.selectedViewController toViewController:childViewController duration:0 options:UIViewAnimationTransitionNone animations:^{} completion:^(BOOL finished) {
+ if (!self.selectedViewController)
self.selectedViewController = childViewController;
- }];
+ else
+ [self transitionFromViewController:self.selectedViewController toViewController:childViewController duration:0 options:UIViewAnimationTransitionNone animations:^{} completion:^(BOOL finished) {
+ self.selectedViewController = childViewController;
+ }];
+
+ if (selector)
+ [self performSelector:selector onViewController:childViewController];
+
}
- (void)setFullScreenMode:(BOOL)moveToFullScreen animated:(BOOL)animated
@@ -213,6 +220,12 @@ - (void)setFullScreenMode:(BOOL)moveToFullScreen animated:(BOOL)animated
#pragma mark - Private methods
+- (void)performSelector:(SEL)aSelector onViewController:(id)viewController;
+{
+ if ([viewController respondsToSelector:aSelector])
+ objc_msgSend(viewController, aSelector, viewController); // performSelector: generates compiler warnings under ARC
+}
+
- (void)performSelectorOnDelegate:(SEL)aSelector withObject:(id)param1 andObject:(id)param2;
{
if ([delegate_ respondsToSelector:aSelector])
@@ -303,7 +316,10 @@ - (void)barButtonReceivedTouchDown:(UIButton *)sender
if (targetViewController) {
// If the target isn't the current viewController, transition to the new view
if (![self.selectedViewController isEqual:targetViewController])
- [self selectViewController:targetViewController];
+
+ // Leave the invocation of the selector for the touch up inside event
+ // TODO: This creates a weird visual effect: switching the controller on the touch down, then executing the selector on the touch up. Fix!
+ [self selectViewController:targetViewController invokeSelector:nil];
if (selector) {
// If we have a targetViewController AND a selector for the button, assign them to the UIControlEventTouchUpInside

0 comments on commit 3dadd29

Please sign in to comment.