3

I have a menu based navigation. The menu is a tableView. Everytime a user presses one entry in that table I want to switch to another viewcontroller, and if there is any view pushed I want to clean the navigation stack first.

This is what I am doing

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [(UINavigationController *)self.tabBar.selectedViewController popToRootViewControllerAnimated:YES];


    self.tabBar.selectedIndex = indexPath.row;

}

But

    self.tabBar.selectedIndex = indexPath.row;

Dont let the popToRoot animation finish. Is there any way to know when the animation is done?

Thanks

pdrcabrod
  • 1,467
  • 1
  • 14
  • 22

3 Answers3

5

in your rootViewController , when you rootViewController invoke the - (void)viewDidAppear:(BOOL)animated it means the animation is finish.

You can code in the rootViewControllers - (void)viewDidAppear:(BOOL)animated

If you want must code in your current ViewController , I think it has 2 ways:

1.add a delegate in the rootViewController , when invoke the - (void)viewDidAppear:(BOOL)animated use delegate to sent the message

2.add a notification in the rootViewController,when invoke the - (void)viewDidAppear:(BOOL)animated post a notification . And in your current ViewController you can receive the notification

Guo Luchuan
  • 4,729
  • 25
  • 33
2

The delegate or notification method of the accepted answer works OK but i find this method handier as it doesn't pass any logic to the root view controller which is only the middle destination of the transition.

You can use a Core Animation transaction. This way you have the completion block that navigation controller doesn't offer.

I capture the tabbarcontroller variable before the block becouse if we dont the tabbarcontroller will be nil in the block. It doesn't create any retention cycle becouse the block ends and disappears

[CATransaction begin];
UITabBarController *tabController = self.navigationController.tabBarController;
[CATransaction setCompletionBlock:^{
    tabController.selectedIndex = 2;
}];

[self.navigationController popToRootViewControllerAnimated:YES];

[CATransaction commit];

The answer was copied from Joris Kluivers answer here Completion block for popViewController and it works perfect for me

Community
  • 1
  • 1
buttcmd
  • 641
  • 5
  • 14
  • 1
    This was unpredictable for me - the block was executed much later when I pushed a new view controller – diachedelic Aug 05 '15 at 02:48
  • Block is supposed to be executed when the CATransaction ends and that is when all controllers except rootViewController are popped. Could you try with another transition? for example use [self.navigationController popViewControllerAnimated:YES]; instead of popToRootViewControllerAnimated, or try it without animation. – buttcmd Aug 06 '15 at 06:37
  • popViewControllerAnimated works great but not popToRootViewControllerAnimated – CyberMew Oct 21 '19 at 09:48
0

No answers worked for me, the only one idea worked for me is to track which controller and when presented.
Example: https://github.com/dzenbot/iOSBlocks/blob/master/Source/UIKit/UINavigationController%2BBlock.m

Same technique presented in this answer: https://stackoverflow.com/a/20565780/1400119
the only difference: github - category, answer - subclass

Community
  • 1
  • 1
trickster77777
  • 1,198
  • 2
  • 19
  • 30