iphone - 单击按钮时的poptorootviewcontroller

标签 iphone ios objective-c uinavigationcontroller uitabbarcontroller

现在让我解释清楚,

我在 viewcontroller 中有一个 tabbarcontoller,它是单 View 应用程序项目的主视图 Controller 。

我在 viewcontroller 中添加了一个 tabbarcontroller 作为 subview 。在 tabbarcontroller 中,我添加了两个导航 Controller ,如下图所示,

enter image description here

我添加了三个(名为 First、Second、Third)的 View Controller 作为新文件。

如果我使用以下代码在第一个选项卡中从一个 View Controller 导航到另一个 View Controller ,

third =  [[Third alloc] initWithNibName:@"Third" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:third animated:YES];

然后我通过单击 tabbarcontroller 下方的选项卡切换到第二个选项卡。

进而

如果我通过单击第三个 View Controller 中的按钮切换到下一个选项卡(第一个选项卡),
-(IBAction)switchtab
{
    vc.tabctrl.selectedIndex=0;
    //vc is the main viewcontroller to which the tabbarcontoller(tabctrl) added as subview
}

切换到下一个选项卡后,我应该弹出rootviewcontroller,我尝试了下面的代码
-(IBAction)switchtab
{
    vc.tabctrl.selectedIndex=0;
    [vc.tabctrl.navigationController  popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}

但它跳转到下一个选项卡但 popToViewController 不起作用,有什么建议吗?

最佳答案

如果您的 View Controller 是导航 Controller ,那么当您切换选项卡时,以下代码将更改为 Root View

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([self.tabBarController.selectedViewController isKindOfClass:[UINavigationController class]])
    {
        [(UINavigationController*)self.tabBarController.selectedViewController popToRootViewControllerAnimated:YES];


    }

}

关于iphone - 单击按钮时的poptorootviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19493002/

相关文章:

ios - 您如何直接链接到 iOS 上的应用评论部分

iOS swift : Sound not playing

iphone - 什么是最佳实践 : Customer wants to publish app himself

iphone - 有没有办法在iPhone上的iOS中以编程方式设置UIBackgroundModes?

ios - imageNamed 在 iOS 7 上返回 nil,但在 iOS 8 上不返回

iphone - 与默认 UIButton 颜色匹配的文本颜色 [蓝色]

objective-c - bundle 无效 Mac OS X

ios - 如何使用 WKWebView 点击电话号码

iphone - UISlider 在移动时修改 NSTimer 间隔崩溃应用程序

ios - UIView 作为轮播的项目