I have a root tabbar vc and navigation vc with one home vc in the tabbar index 0, and one another vc in the tabbar index 1;
Now I'm pushing to one subvc and this subvc is shown above on the homevc
Then I triggered using button to pop this subvc out and get back to homevc using popToRootViewController
and then go to the index 1 vc immediately.
Now the problem is that, if I did not go to index 1 vc after pop to root, the subvc would be popped and its viewDidDisappear
would be called as usual, however, if I go to index 1 or other index for the root tabbar after pop to root, the subvc would NOT call viewDidDisappear
when it was being popped.
I know my description is confusing so here is the code:
//In this version, the subVC's viewDidDisappear would NOT be called
[self.subVC popToRootViewControllerAnimated:NO]; //self is kind of UITabbarVController
self.selectedIndex = 1; //switch to other tab
//In this one, the subVC's viewDidDisappear would be called as usual
[self.subVC popToRootViewControllerAnimated:NO]; //self is kind of UITabbarVController
So as you see, the difference is only self.selectedIndex = 1;
which caused my subVC's viewDidDisappear
cannot be called.
But I get a solution like below:
//In this version, the main idea is to skip two runloops
//to execute self.selectedIndex = 1;
//so the subVC's viewDidDisappear would be called
[self.subVC popToRootViewControllerAnimated:NO];
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
self.selectedIndex = 1;
});
});
So who can explain the root cause behind it?