0

我有一个带有导航栏和横幅视图委托的程序化标签栏委托。对于我的生活,我似乎无法让标签在点击时弹出根视图。我知道我需要使用类似 [self.navigationController popToRootViewControllerAnimated:NO]; 但我不知道把它放在我的应用程序委托中的哪里。

@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 吗?我最后试过了,但它似乎没有抓住任何控制器......

4

2 回答 2

0

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

_tabBarConroller.delegate = self;

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

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

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

于 2012-12-08T18:28:40.830 回答
0

如果你想在你的 tabBar 中添加导航控制器,那么你必须添加你的导航控制器而不是 viewController。因此,在您的情况下,您没有将 navigationControllers 添加到 tabBarControllers 数组中。

[viewControllers addObject:bannervcs]; 

所以而不是添加bannervcs添加newsNavigationController

[viewControllers addObject:newsNavigationController];  

有关更多信息,请阅读并获取UITabBarController 类参考中的示例代码

于 2012-12-10T07:27:21.437 回答