从ViewController调用poptoviewcontroller函数 [英] Calling poptoviewcontroller function from a viewcontroller
问题描述
让我解释清楚.
在viewcontroller中有一个tabbarcontoller,它是单视图应用程序项目的主视图控制器.
I have a tabbarcontoller in the viewcontroller which is the main view controller of the single view application project.
我在视图控制器中添加了一个tabbarcontroller作为子视图.在tabbarcontroller中,我添加了两个导航控制器,如下图所示,
I added a tabbarcontroller to the viewcontroller as the subview. In the tabbarcontroller, I added two navigation controllers like below image,
我已经添加了三个(分别名为First,Second,Third)的视图控制器作为新文件.
I have added three(named First, Second, Third) more viewcontrollers as new file.
如果我使用下面的选项卡从一个视图控制器导航到另一个视图控制器,
If I navigate from one viewcontroller to other in a tab using below,
third = [[Third alloc] initWithNibName:@"Third" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:third animated:YES];
如果我切换到下一个选项卡并返回上一个选项卡,它应该会弹出到上一个视图控制器,该怎么做?
If I switch to next tab and come back to the previous tab, it should popto the previous view controller, how to do this?
我尝试过
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
不成功
我也尝试过
[third popto];
在第三个ViewController中,
In the third viewcontroller,
-(void)popto
{
[self.navigationController popViewControllerAnimated:YES];
}
什么都没发生.
现在,我必须再次单击该选项卡,将poptoviewcontroller弹出到第一个ViewController.
任何想法都将受到高度赞赏.
Any ideas will be highly appreciated.
推荐答案
尝试以下代码段
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)viewController;
if (navController && ([[navController viewControllers] count] > 0))
{
[navController popToRootViewControllerAnimated:NO];
}
return YES;
}
希望它可能对您有用.
这篇关于从ViewController调用poptoviewcontroller函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!