iOS: popViewController unexpected behavior

后端 未结 7 1890
迷失自我
迷失自我 2020-12-02 16:52

I\'ve been searching the internet for a solution. There\'s nothing I could find. So: I\'m using a UINavigationController. I am pushing two UIViewControllers onto it. In the

相关标签:
7条回答
  • 2020-12-02 17:37

    Combining MilGra and Andrew's answers gave me something that works reliably and is a simpler drop-in UINavigationController replacement.

    This improves on MilGra's answer to make it work with pushes and pops, but is simpler than Andrew's BufferedNavigationController. (Using BufferedNavigationController I was occasionally getting transitions that would never complete and would only show a black screen.)

    This whole thing seems not to be necessary on iOS8, but was still needed for me on iOS7.

    @implementation UINavigationControllerWithQueue {
        NSMutableArray *waitingList;
    }
    
    -(void) viewDidLoad {
        [super viewDidLoad];
        self.delegate = self; // NOTE: delegate must be self!
        waitingList = [[NSMutableArray alloc] init];
    }
    
    # pragma mark - Overrides
    
    -(void) pushViewController: (UIViewController*) controller
                      animated: (BOOL) animated {
        [self queueTransition:^{ [super pushViewController:controller animated:animated]; }];
    }
    
    - (UIViewController *)popViewControllerAnimated:(BOOL)animated {
        UIViewController *result = [self.viewControllers lastObject];
        [self queueTransition:^{ [super popViewControllerAnimated:animated]; }];
        return result;
    }
    
    - (NSArray*)popToRootViewControllerAnimated:(BOOL)animated {
        NSArray* results = [self.viewControllers copy];
        [self queueTransition:^{ [super popToRootViewControllerAnimated:animated]; }];
        return results;
    }
    
    # pragma mark - UINavigationControllerDelegate
    
    -(void) navigationController: (UINavigationController*) navigationController
           didShowViewController: (UIViewController*) controller
                        animated: (BOOL) animated {
        [self dequeTransition];
    }
    
    # pragma mark - Private Methods
    
    -(void) queueTransition:(void (^)()) transition {
        [waitingList addObject:transition];
        if (waitingList.count == 1) {
            transition();
        }
    }
    
    -(void) dequeTransition {
        if (waitingList.count > 0) {
            [waitingList removeObjectAtIndex:0];
        }
        if (waitingList.count > 0) {
            void (^transition)(void) = [waitingList objectAtIndex:0];
            if (transition) {
                transition();
            }
        }
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题