UITabBarController setSelectedIndex 性能低下

UITabBarController setSelectedIndex slow performance

当用户按下中心 UITabBarItem 我会呈现一个模式 UIView。把它想象成 Instagram。

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

if(viewController == [self.viewControllers objectAtIndex:2])
{
    CameraViewController *cameraVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"cameraVC"];
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:cameraVC];
    navController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    [self presentViewController:navController animated:YES completion:nil];
    return NO;
}
else
{
    return YES;
}
}

这非常有效。

当我在 CameraViewController 中完成拍照后,我希望关闭视图并选择第 4 个 UITabBarItem 作为图片结果 (HistoryViewController)。

这就是我在 CameraViewController(模态推送)中的做法:

[self dismissViewControllerAnimated:YES completion:nil];

[(UITabBarController *)self.presentingViewController setSelectedIndex:3];

这就是问题所在。

如您所见,第 4 个选项卡中的 text 已被选中,但第一个选项卡 icon 仍处于选中状态。此外,显示的 view 是第一个选项卡中的视图。

大约 10 秒后,它最终将视图更改为正确的第 4 个选项卡。

我试图找出是什么进程造成了这种减速,所以我设置了很多 NSLog。

大约 10 秒的减速介于 CameraViewController 中的 [(UITabBarController *)self.presentingViewController setSelectedIndex:3]; 和 HistoryViewController 中的 viewDidLoad 之间。

在这些 calls/methods 之间发生了什么可能导致减速?

编辑:

在 CameraViewController 中:

- (void)scan {
dispatch_queue_t scanTesseract = dispatch_queue_create("scanTesseract", NULL);

dispatch_async(scanTesseract, ^(void) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD setForegroundColor:[UIColor ht_mintDarkColor]];
        [SVProgressHUD showProgress:0 status:@"Scanning"];
    });

    //background processing goes here
    [self.tesseract setImage:self.imgToScan.blackAndWhite];
    [self.tesseract recognize];
    [self filterResults:[self.tesseract recognizedText]];
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
    [self scanningDone];
});
}

- (void)scanningDone {
[LastScan getInstance].hasBeenViewed = FALSE;

[self dismissViewControllerAnimated:YES completion:nil];

[(UITabBarController *)self.presentingViewController setSelectedIndex:3];
}

在 HistoryViewController 中:

- (void)viewDidLoad {
[super viewDidLoad];

NSLog(@"ViewDidLoad");
self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;

self.collectionView.backgroundColor = [UIColor whiteColor];
}

这个呢?

[self dismissViewControllerAnimated:YES completion:^{
    [(UITabBarController *)self.presentingViewController setSelectedIndex:3];
}];

您正在后台队列中调用 scanningDone。在主队列上执行该方法。

dispatch_async(dispatch_get_main_queue(), ^{
   [self scanningDone];
});