在TabBarController和ViewController之间切换的最佳方法

时间:2013-04-19 20:13:15

标签: ios objective-c uiviewcontroller uitabbarcontroller

我一直在尝试使用初始登录屏幕的应用,然后转到TabBarController 我想知道什么是最好的方法来做任何示例代码将不胜感激。我试过了,但我无法从ViewController切换到TabController

2 个答案:

答案 0 :(得分: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;
}

我通常不希望将此类代码放在app委托中,但如果它是登录详细信息之类的一次性内容,也许就可以了。

enter image description here

答案 1 :(得分: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