|
| 1 | +// |
| 2 | +// StackNavigationController.m |
| 3 | +// StackNavigationController |
| 4 | +// |
| 5 | +// Created by Igor Kamenev on 25.12.14. |
| 6 | +// Copyright (c) 2014 Igor Kamenev. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | + |
| 10 | +#import "StackNavigationController.h" |
| 11 | + |
| 12 | +@interface StackNavigationController () <UINavigationControllerDelegate> |
| 13 | + |
| 14 | +@property (nonatomic, assign) BOOL isTransitioning; |
| 15 | +@property (nonatomic, strong) NSMutableArray *tasks; |
| 16 | +@property (nonatomic, weak) id<UINavigationControllerDelegate> customDelegate; |
| 17 | + |
| 18 | +@end |
| 19 | + |
| 20 | +@implementation StackNavigationController |
| 21 | + |
| 22 | +-(void)viewDidLoad { |
| 23 | + [super viewDidLoad]; |
| 24 | + |
| 25 | + if (self.delegate) { |
| 26 | + self.customDelegate = self.delegate; |
| 27 | + } |
| 28 | + self.delegate = self; |
| 29 | + |
| 30 | + self.tasks = [NSMutableArray new]; |
| 31 | +} |
| 32 | + |
| 33 | +-(void)setDelegate:(id<UINavigationControllerDelegate>)delegate |
| 34 | +{ |
| 35 | + if (delegate == self) { |
| 36 | + [super setDelegate:delegate]; |
| 37 | + } else { |
| 38 | + self.customDelegate = delegate; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated { |
| 44 | + |
| 45 | + @synchronized(self.tasks) { |
| 46 | + if (self.isTransitioning) { |
| 47 | + |
| 48 | + void (^task)(void) = ^{ |
| 49 | + [self pushViewController:viewController animated:animated]; |
| 50 | + }; |
| 51 | + |
| 52 | + [self.tasks addObject:task]; |
| 53 | + } |
| 54 | + else { |
| 55 | + self.isTransitioning = YES; |
| 56 | + [super pushViewController:viewController animated:animated]; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +-(UIViewController *)popViewControllerAnimated:(BOOL)animated |
| 62 | +{ |
| 63 | + @synchronized(self.tasks) { |
| 64 | + if (self.isTransitioning) { |
| 65 | + |
| 66 | + void (^task)(void) = ^{ |
| 67 | + [self popViewControllerAnimated:animated]; |
| 68 | + }; |
| 69 | + |
| 70 | + [self.tasks addObject:task]; |
| 71 | + |
| 72 | + return nil; |
| 73 | + |
| 74 | + } else { |
| 75 | + |
| 76 | + self.isTransitioning = YES; |
| 77 | + return [super popViewControllerAnimated:animated]; |
| 78 | + |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +-(NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated |
| 84 | +{ |
| 85 | + @synchronized(self.tasks) { |
| 86 | + if (self.isTransitioning) { |
| 87 | + |
| 88 | + void (^task)(void) = ^{ |
| 89 | + [self popToViewController:viewController animated:animated]; |
| 90 | + }; |
| 91 | + |
| 92 | + [self.tasks addObject:task]; |
| 93 | + |
| 94 | + return nil; |
| 95 | + |
| 96 | + } else { |
| 97 | + self.isTransitioning = YES; |
| 98 | + return [super popToViewController:viewController animated:animated]; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +-(NSArray *)popToRootViewControllerAnimated:(BOOL)animated |
| 104 | +{ |
| 105 | + @synchronized(self.tasks) { |
| 106 | + if (self.isTransitioning) { |
| 107 | + |
| 108 | + void (^task)(void) = ^{ |
| 109 | + [self popToRootViewControllerAnimated:animated]; |
| 110 | + }; |
| 111 | + |
| 112 | + [self.tasks removeAllObjects]; |
| 113 | + [self.tasks addObject:task]; |
| 114 | + |
| 115 | + return nil; |
| 116 | + |
| 117 | + } else { |
| 118 | + self.isTransitioning = YES; |
| 119 | + return [super popToRootViewControllerAnimated:animated]; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +- (void) runNextTask { |
| 125 | + |
| 126 | + @synchronized(self.tasks) { |
| 127 | + if (self.tasks.count) { |
| 128 | + void (^task)(void) = self.tasks[0]; |
| 129 | + [self.tasks removeObjectAtIndex:0]; |
| 130 | + task(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +#pragma mark UINavigationControllerDelegate |
| 136 | + |
| 137 | +-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated |
| 138 | +{ |
| 139 | + self.isTransitioning = NO; |
| 140 | + |
| 141 | + if (self.customDelegate) { |
| 142 | + [self.customDelegate navigationController:navigationController didShowViewController:viewController animated:animated]; |
| 143 | + } |
| 144 | + |
| 145 | + [self performSelector:@selector(runNextTask) withObject:nil afterDelay:0.0f]; |
| 146 | +} |
| 147 | + |
| 148 | +@end |
0 commit comments