如何将popToRootViewControllerAnimated与我的标签集成? [英] How do I integrate popToRootViewControllerAnimated with my tabs?

查看:93
本文介绍了如何将popToRootViewControllerAnimated与我的标签集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用popToRootViewControllerAnimated使我的一个标签按钮转到根目录。我的问题是:我在哪里放这个代码才能工作?我通过Interface Builder创建了标签...是否必须对其进行硬编码以使其正常工作?

I am trying to make one of my tab buttons go to the root using popToRootViewControllerAnimated. My question is: where do I put this code for it to work? I have my tabs created through Interface Builder... do they have to be hard coded for this to work?

以下是我要使用的代码:

Here is the code that I'm looking to use:

[self.navigationController popToRootViewControllerAnimated:YES];

AppDelegate中的新代码:

New code in AppDelegate:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (viewController = HomeViewController) {
        [HomeViewController popToRootViewControllerAnimated:NO];
    }
}


推荐答案

亚当 - 我最终放弃了子类的想法,即使它有效,因为有一个更简单的方法。

Adam - I ended up ditching the subclass idea even though it worked, as there is a much easier method.

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

这是所需的代码。我已经上传了这个示例项目。要点是

This is the code required. I've uploaded this sample project to play around with. The main points are


  • UITabBarController委托必须将
    设置为App Delegate。

  • App
    代表必须实现< UITabBarControllerDelegate> 协议。

  • App Delegate必须
    实现上面的代码。

  • the UITabBarController delegate must be set to the App Delegate.
  • the App Delegate must implement the <UITabBarControllerDelegate> protocol.
  • the App Delegate must implement the code above.

示例项目还显示了一种选择性地选择使用哪种导航控制器的方法。

The sample project also shows one way to selectively choose which navigation controllers this occurs with.

这篇关于如何将popToRootViewControllerAnimated与我的标签集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆