2

据我所知,似乎UINavigationController用动画背靠背推入在 iOS7 上造成了死锁。

我最初在 iOS6 上遇到崩溃并想出了以下解决方案:

  1. 创建一个视图控制器数组以在初始推送后推送到导航控制器。因此,如果要推送三个视图控制器(A、B 和 C),则该数组将包含 B 和 C。

  2. 实现UINavigationControllerDelegatenavigationController:didShowViewController:animated:

  3. 在委托方法中,只需检查视图控制器数组中是否有更多元素。如果是这样,从其中取出第一个元素并将其推送到导航控制器中。所以本质上如果有 B 和 C 视图控制器要推送, navigationController:didShowViewController:animated:会被调用 3 次;每次从 A 开始推送视图控制器之后。显然,最后一次调用不会做任何事情,因为此时数组应该是空的。

请注意,这种方法在 iOS6 上运行良好。但是,这在 iOS7 中中断了。似乎当它尝试在第二次推送中设置动画时,应用程序冻结了。在深入挖掘之后,我想出了在委托实现中以以下方式推送第二个视图控制器的解决方案。

dispatch_async(dispatch_get_main_queue(), ^{
    [navigationController pushViewController:viewController 
                                    animated:YES];
});

这似乎解决了这个问题,但我想知道是否有人经历过类似的事情并对到底发生了什么有更好的解释。

4

1 回答 1

0

I'm not sure what problem you're having. I created a test app that has a navigation controller and four other controllers. The first controller has a button to push to the second controller, which is FirstPushedViewController. In that controller I have this code to push the next two controllers, and it worked fine in iOS 7:

@interface FirstPushedViewController ()
@property (strong,nonatomic) NSMutableArray *vcs;
@end

@implementation FirstPushedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
    UIViewController *orange = [self.storyboard instantiateViewControllerWithIdentifier:@"Orange"];
    UIViewController *red = [self.storyboard instantiateViewControllerWithIdentifier:@"Red"];
    self.vcs = [@[red,orange] mutableCopy];
}

-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.vcs.count > 0) {
        [self.navigationController pushViewController:self.vcs.lastObject animated:YES];
        [self.vcs removeLastObject];
    }
}
于 2013-11-01T02:14:49.243 回答