问题描述
我有使用情节提要的带有导航控制器应用程序的标签栏,
我的目的是在 tab3 中按下一个按钮,在后台我希望 tab1 为popToRootViewController"
I have tab bar with navigation controller app using storyboard ,
my purpose is to press a button in tab3 and in the background I want tab1 to "popToRootViewController"
tab3 视图控制器中的按钮:
the button in tab3 viewcontroller:
- (IBAction)Action:(id)sender {
vc1 * first = [[vc1 alloc]init];
[first performSelector:@selector(popToRootViewController) withObject:Nil];
}
tab1 viewcontroller 中的代码
the code in the tab1 viewcontroller
-(void)popToRootViewController{
[self.navigationController popToRootViewControllerAnimated:NO];
NSLog(@"popToRootViewController");
}
我在日志中得到了 popToRootViewController
,但操作没有执行.
I get the popToRootViewController
in logs, but the action didn't perform.
解决问题:
- (IBAction)Action:(id)sender {
[[self.tabBarController.viewControllers objectAtIndex:0]popToRootViewControllerAnimated:NO];
}
推荐答案
你的做法:
vc1 * first = [[vc1 alloc]init];
[first performSelector:@selector(popToRootViewController) withObject:Nil];
不正确.事实上,您在这里创建了一个全新的控制器,完全独立于您现有的控制器,不属于任何导航控制器.因此,self.navigationController
在 popToRootViewController
中是 nil
.
is not correct. Indeed, you are creating a whole new controller here, completely independent from your existing controllers and not belonging to any navigation controller. For this reason, self.navigationController
is nil
in popToRootViewController
.
您可以尝试执行以下操作:
You might try doing something like:
//-- this will give you the left-most controller in your tab bar controller
vc1 * first = [self.tabBarController.viewControllers objectAtIndex:0];
[first performSelector:@selector(popToRootViewController) withObject:Nil];
这篇关于从另一个选项卡调用 popToRootViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!