【问题标题】:Disable/hide root view controller for one view controller禁用/隐藏一个视图控制器的根视图控制器
【发布时间】:2014-07-20 15:05:03
【问题描述】:

我在隐藏 UITabBarController 时遇到了麻烦,我将其定义为整个应用程序的 rootViewController

我试图在显示的第一个视图上隐藏 UITabBarController - 它是整个应用程序的根视图控制器。这个想法是第一个视图有UIImageView 实例,它们跳转到定义的UIViewControllers(也被定义为根UITabBarController 的视图控制器)。

有没有办法让第一个视图控制器没有根 UITabBarController,但为所有其他定义为 viewControllers 的视图保留它?

这是AppDelegate 中的代码,将视图控制器和 UITabBarController 定义为 rootViewController。

- (void)initViewControllers {
    anIdeaVC = [[IdeaViewController alloc] initWithNibName:@"IdeaViewController" bundle:nil];
    [anIdeaVC setTabBarItem:[[[UITabBarItem alloc] initWithTitle:@"Idea" image:[UIImage imageNamed:@"iconIdee.png"] tag:0] autorelease]];

    aListTableVC = [[ListTableViewController alloc] initWithStyle:UITableViewStylePlain];
    [aListTableVC setTitle:@"List"];

    aListNC = [[ListNavigationController alloc] initWithRootViewController:aListTableVC];
    [aListNC setTabBarItem:[[[UITabBarItem alloc] initWithTitle:@"List" image:[UIImage imageNamed:@"iconList.png"] tag:0] autorelease]];

    anInnMapVC = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];

    anInnMapNC = [[InnMapNavigationController alloc] initWithRootViewController:anInnMapVC];
    [anInnMapNC setTabBarItem:[[[UITabBarItem alloc] initWithTitle:@"InnMap" image:[UIImage imageNamed:@"iconInnMap.png"] tag:0] autorelease]];

    aSearchTableVC = [[SearchTableViewController alloc] initWithNibName:@"SearchTableViewController" bundle:nil];
    [aSearchTableVC setTitle:@"Search"];

    aSearchNC = [[SearchNavigationController alloc] initWithRootViewController:aSearchTableVC];
    [aSearchNC setTabBarItem:[[[UITabBarItem alloc] initWithTitle:@"Search" image:[UIImage imageNamed:@"iconSearch.png"] tag:0] autorelease]];

    tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:[NSArray arrayWithObjects:anIdeaVC, aListNC, anInnMapNC, aSearchNC, nil] animated:NO];
    [tabBarController setSelectedViewController:anIdeaVC];
    [tabBarController setDelegate:self];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    [self initViewControllers];

    [window setRootViewController:tabBarController];
    [window makeKeyAndVisible];

    return YES;
}

提前感谢您的帮助:-)。

【问题讨论】:

  • 你能描述一下功能目标吗?
  • 在定义的视图中隐藏UITabBarController(即应用程序的rootViewController) - 但不将该定义的视图添加到UITabBarController 本身。
  • 我正在尝试获取您希望用户看到的内容。可能有一种方法可以在没有太多体操的情况下做到这一点。例如标签栏 vc 不必一直是根目录。
  • 用户打开应用,看到一个视图没有UITabBarController点击了这个视图中的一个图标(所有这些图标都指向一个定义为视图控制器的UIViewController在根视图控制器(= UITabBarController))。所有可以从第一个视图控制器访问的视图控制器的底部都有 UITabBarController。
  • 好的 - 每次应用启动时都应该发生这种情况吗? (当它在后台时怎么样?)您真正要求的是 not 有时将标签栏 vc 作为根。这个带有图标的其他 vc 既可以呈现它(在其自身之上),也可以在用户选择后被它替换。我可以向您展示其中任何一个的代码,具体取决于更改发生的确切时间和频率。

标签: ios objective-c ios7


【解决方案1】:

我认为解决此问题的最佳方法是将带有图标的 vc 设置为根目录。然后,当用户进行选择时,创建标签栏 vc 并使其成为根目录。

创建一个视图控制器(不仅仅是一个视图)来显示图标并获得用户选择。在启动时设置窗口的根...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // don't do this
    //[self initViewControllers];

    // or this
    //[window setRootViewController:tabBarController];

    // instead do this, create the vc that lets user select an icon
    // put your icon view in there
    IconSelectVC *iconSelectVC = [[IconSelectVC alloc] init];
    [window setRootViewController:iconSelectVC];
    [window makeKeyAndVisible];

    return YES;
}

initViewControllers 方法添加到您的应用程序委托的公共接口,以便可以从IconSelectVC 调用它。然后在其中添加最后一行,使其替换窗口的根 vc。

    // ... the rest of initViewControllers, then
    [tabBarController setSelectedViewController:anIdeaVC];
    [tabBarController setDelegate:self];

    [window setRootViewController:tabBarController];
}

现在,当您决定更改 UI 时,在 IconSelectVC 中获取应用委托单例并更改窗口的根目录。

// in IconSelectVC.m
// when you decide to change to the tab bar.

// Be aware that this vc will be released here, so do any cleaning you need to do here
// e.g. unsubscribe from NSNotifications, clean any timers, finish any asynch requests, etc. 

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate initViewControllers];

编辑 - 我们没有讨论这个过渡应该是什么样子 - 我在这里的建议会导致一个“丑陋”的过渡(当然,在旁观者的眼中),其中 UI 只是在一帧中发生变化。获得更好过渡的一种方法(其中一种)是使用 os7 自定义 vc 过渡。

【讨论】:

  • 谢谢 - 这真的帮助了我。我可以使用它:-)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 2014-03-28
相关资源
最近更新 更多