2

我有一个根标签栏 vc 和导航 vc,其中一个 home vc 在标签栏索引 0 中,另一个 vc 在标签栏索引 1 中;

现在我推送到一个 subvc,这个 subvc 显示在 homevc 上面

然后我触发了使用按钮弹出这个 subvc 并使用返回到 homevcpopToRootViewController然后立即转到索引 1 vc。

现在的问题是,如果我在 pop 到 root 后没有转到索引 1 vc,那么 subvc 将被弹出并且它viewDidDisappear会像往常一样被调用,但是,如果我在 pop 后转到 root tabbar 的索引 1 或其他索引对于 root,subvc 在弹出时不会调用viewDidDisappear

我知道我的描述令人困惑,所以这里是代码:

//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

如您所见,区别仅self.selectedIndex = 1;在于导致viewDidDisappear无法调用我的 subVC。

但我得到如下解决方案:

//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;
   });
});

那么谁能解释其背后的根本原因呢?

4

0 回答 0