无法获取tabbar app到popToRootViewController

时间:2012-12-08 18:20:19

标签: iphone ios xcode uinavigationcontroller uitabbarcontroller

我有一个带有导航栏的程序化tabbar delagate,带有横幅视图委托。而对于我的生活,我似乎无法在单击时弹出根视图的选项卡。我知道我需要使用类似[self.navigationController popToRootViewControllerAnimated:NO];但是我不知道把它放在我的app委托中。

@implementation AppDelegate {
UITabBarController *_tabBarController;
  }


 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:      (NSDictionary *)launchOptions
{
NSError *setCategoryError = nil;

CGRect bounds = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:bounds];
self.window.backgroundColor = [UIColor whiteColor];

NSMutableArray * viewControllers = [[NSMutableArray alloc] init];

NSString * subscriptionListFile = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Subscription.plist"];

NSDictionary * subscriptionList = [[NSDictionary alloc] initWithContentsOfFile:subscriptionListFile];

NSArray * subscriptionFolders = subscriptionList[@"Folders"];

NewsListViewController * newsController = nil;
UINavigationController * newsNavigationController = nil;
BannerViewController * bannervcs = nil;

for (NSDictionary * folderDetails in subscriptionFolders) {

    NSArray * newsItems = folderDetails[@"Items"];
    NSString * folderTitle = folderDetails[@"FolderName"];
    NSString * folderIcon = folderDetails[@"FolderIcon"];
    UIImage * folderIconImage = [UIImage imageNamed:folderIcon];

    newsController = [[NewsListViewController alloc] initWithNewsSourceList:newsItems];
    [newsController setTitle:folderTitle];

    newsNavigationController = [[UINavigationController alloc] initWithRootViewController:newsController];
    [newsNavigationController setTitle:folderTitle];

    bannervcs = [[BannerViewController alloc] initWithContentViewController:newsNavigationController];
    [bannervcs.tabBarItem setImage:folderIconImage];
    [viewControllers addObject:bannervcs];

  }


_tabBarController = [[UITabBarController alloc] init];
_tabBarController.viewControllers = viewControllers;

   self.window.rootViewController = _tabBarController;
   [self.window makeKeyAndVisible];

    return YES;
}



 @end

知道我在哪里添加popToRootViewController吗?我最后试过但它似乎没有抓住任何控制器...

2 个答案:

答案 0 :(得分:0)

你需要设置一些东西作为你的UITabBarControllerDelegate。假设您要在AppDelegate上尝试此操作,请将该行添加到您的方法中:

_tabBarConroller.delegate = self;

然后,将此方法添加到AppDelegate(因为它现在也被视为UITabBarControllerDelegate)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    viewController.navigationController popToRootViewControllerAnimated:NO];
}

这假设,无论用户在选项卡上离开导航堆栈的状态如何,点击另一个选项卡始终会将用户带到导航堆栈的根视图控制器。

答案 1 :(得分:0)

如果你想在tabBar中使用navigationController,那么你必须添加yourNavigationController而不是viewController。 因此,在您的情况下,您不会将navigationControllers添加到tabBarControllers数组中。

[viewControllers addObject:bannervcs]; 

所以不要添加bannervcs添加newsNavigationController

[viewControllers addObject:newsNavigationController];  

有关详细信息,请阅读UITabBarController Class Reference

中的示例代码