Permalink
Browse files

[FIX #16] Pilot now takes two init methods that store either the nav …

…controller or the tabbar controller and then tries to do the right thing.
  • Loading branch information...
1 parent f6a4c1b commit dc627eff9d711d386ce995eb6f1c5d47746fd294 Arik Devens committed Sep 24, 2011
Showing with 44 additions and 16 deletions.
  1. +3 −2 Pilot/PTApplicationDelegate.m
  2. +10 −6 Pilot/Pilot.h
  3. +31 −8 Pilot/Pilot.m
@@ -20,13 +20,14 @@ - (void)dealloc {
[super dealloc];
}
-- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-
+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
+ [Pilot initWithNavigationController:self.navigationController];
+
return YES;
}
View
@@ -18,6 +18,16 @@
/**
*
*/
++ (void)initWithNavigationController:(UINavigationController *)navigationController;
+
+/**
+ *
+ */
++ (void)initWithTabBarController:(UITabBarController *)tabBarController;
+
+/**
+ *
+ */
+ (void)showObject:(PTObject *)object;
/**
@@ -60,10 +70,4 @@
*/
+ (void)showObject:(PTObject *)object withSelector:(SEL)selector animation:(UIViewAnimationTransition)animation asModal:(BOOL)asModal;
-/**
- * Returns the UINavigationController instance on the applicationDelegate
- * @see applicationDelegate
- */
-+ (UINavigationController *)navigationController;
-
@end
View
@@ -14,8 +14,12 @@ + (Class)viewControllerClassForObject:(PTObject *)object;
+ (SEL)defaultInitializer;
+ (void)presentViewController:(id)viewController withAnimation:(UIViewAnimationTransition)animation;
+ (void)presentViewControllerAsModal:(id)viewController withAnimation:(UIViewAnimationTransition)animation;
++ (UINavigationController *)currentNavigationController;
@end
+static UINavigationController *rootNavigationController = nil;
+static UITabBarController *rootTabBarController = nil;
+
@implementation Pilot
#pragma mark - Private Methods
@@ -34,15 +38,37 @@ + (SEL)defaultInitializer {
}
+ (void)presentViewController:(id)viewController withAnimation:(UIViewAnimationTransition)animation {
- [[self navigationController] pushViewController:viewController animated:YES];
+ [[self currentNavigationController] pushViewController:viewController animated:YES];
}
+ (void)presentViewControllerAsModal:(id)viewController withAnimation:(UIViewAnimationTransition)animation {
- [[self navigationController] presentModalViewController:viewController animated:YES];
+ [[self currentNavigationController] presentModalViewController:viewController animated:YES];
+}
+
++ (UINavigationController *)currentNavigationController {
+ if (rootNavigationController) {
+ return rootNavigationController;
+ } else {
+ UIViewController *navigationController = rootTabBarController.selectedViewController;
+
+ if ([navigationController isKindOfClass:[UINavigationController class]]) {
+ return (UINavigationController *)navigationController;
+ }
+ }
+
+ return nil;
}
#pragma mark - Public API
++ (void)initWithNavigationController:(UINavigationController *)navigationController {
+ rootNavigationController = navigationController;
+}
+
++ (void)initWithTabBarController:(UITabBarController *)tabBarController {
+ rootTabBarController = tabBarController;
+}
+
+ (void)showObject:(PTObject *)object {
[self showObject:object withSelector:[self defaultInitializer] animation:UIViewAnimationTransitionNone asModal:NO];
}
@@ -78,6 +104,9 @@ + (void)showObjectAsModal:(PTObject *)object withSelector:(SEL)selector animatio
+ (void)showObject:(PTObject *)object withSelector:(SEL)selector animation:(UIViewAnimationTransition)animation asModal:(BOOL)asModal {
Class viewControllerClass = [self viewControllerClassForObject:object];
+ NSAssert([viewControllerClass instancesRespondToSelector:selector], @"PILOT ERROR: Could not find selector %@ for %@ViewController",
+ NSStringFromSelector(selector), NSStringFromClass([object class]));
+
NSAssert([object respondsToSelector:@selector(identifier)], @"PILOT ERROR: Could not find an instance variable named identifier on object %@",
NSStringFromClass([object class]));
@@ -90,10 +119,4 @@ + (void)showObject:(PTObject *)object withSelector:(SEL)selector animation:(UIVi
}
}
-#pragma mark - Accessors
-
-+ (UINavigationController *)navigationController {
- return [PTApplicationDelegate sharedDelegate].navigationController;
-}
-
@end

0 comments on commit dc627ef

Please sign in to comment.