1

我一直在尝试一个带有初始登录屏幕的应用程序,然后进入TabBarController.
我想知道执行此操作的最佳方法是什么,任何示例代码都将不胜感激。我已经尝试过了,但我无法从 切换ViewControllerTabController.

4

2 回答 2

1

假设您的根视图控制器也是您的登录视图。

现在从您的根视图控制器中,您可以通过多种方式呈现标签栏控制器。一种方法是只presentViewController从根视图控制器调用该方法。

设置

在根视图控制器中,有时在显示标签栏之前,设置它:

myTabBarViewController = [[MyTabBarViewController alloc] init];
[myTabBarViewController setModalPresentationStyle:UIModalPresentationFullScreen];
[myTabBarViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[myTabBarViewController setRootTabBarDelegate:self];

介绍

当您准备好展示时,只需调用以下代码:

[self presentViewController:myTabBarViewController animated:YES completion:nil];

笔记

视图控制器层次结构如下所示:

AppDelegate
  L RootViewController
      L MyTabBarController
于 2013-04-19T21:42:53.157 回答
1

我不确定这是不是最好的方法,但它又快又脏并且有效。applicationDidFinishLaunchineWithOptions:在您的方法中显示一个模态视图控制器。你应该用@selector更适合你想做的事情来替换。背景颜色仅用于效果。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[viewController1, viewController2];
    self.window.rootViewController = self.tabBarController;


    [self.window makeKeyAndVisible];


    // ***** The relevant code *****
    UIViewController *viewController = [[UIViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [[viewController view] setBackgroundColor:[UIColor redColor]];
    UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [dismissButton setFrame:CGRectMake(10, 10, 300, 44)];
    [dismissButton setTitle:@"Dismiss" forState:UIControlStateNormal];
    [dismissButton addTarget:[self tabBarController] action:@selector(dismissModalViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    [[viewController view] addSubview:dismissButton];
    [[self tabBarController] presentViewController:navigationController animated:NO completion:nil];

    return YES;
}

我通常不喜欢将这种代码放在应用程序委托中,但如果它是一次性的东西,比如登录详细信息,也许没关系。

在此处输入图像描述

于 2013-04-19T20:59:44.130 回答