如何在app delegate中创建Tab Bar控制器

时间:2012-12-05 16:17:48

标签: objective-c ios cocoa-touch ios5 ios4

我正在尝试执行以下操作:

  1. 在应用中显示启动页面。
  2. 尝试通过ping我的网站登录用户。如果用户已登录,请转换为每个选项卡带有3个选项卡和导航控制器的选项卡控制器。
  3. 如果用户未登录,请使用3个标签转换到标签控制器,但每个标签会将用户带到不同的页面(登录,注册,了解详情)
  4. 如何在视图控制器中添加选项卡控制器,而不是在应用程序委托中添加?

    我打算做以下事情

    应用程序委托中的

    将rootController设置为viewcontroller1

    UIViewController * viewController1;
    viewController1 = [[HTLoginViewController alloc] initWithNibName:@"HTLoginViewController_iPhone" bundle:nil];
    self.window.rootViewController = viewController1;
    

    然后在viewcontroller1中调用

    下面的setTab函数

    - (void)setTab {     self.tabBarController = [[UITabBarController alloc] init];

    UIViewController * viewController1, *viewController2, *viewController3;
    
    viewController1 = [[HTMyJobsViewController alloc] initWithNibName:@"HTMyJobsViewController" bundle:nil];
    viewController2 = [[MTViewController alloc] initWithNibName:@"MTViewController" bundle:nil];
    viewController3 = [[HTInviteController alloc] initWithNibName:@"HTInviteController" bundle:nil];
    
    
    viewController1.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
    
    
    // account view
    UINavigationController * firstNavController = [[UINavigationController alloc]initWithRootViewController: viewController1];
    viewController2.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
    
    // book view
    UINavigationController * secondNavController = [[UINavigationController alloc]initWithRootViewController: viewController2];
    viewController3.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
    
    // invite view
    UINavigationController * thirdNavController = [[UINavigationController alloc]initWithRootViewController: viewController3];
    
    
    CATransition *transition = [CATransition animation];
    transition.duration = 0.7f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    
    [firstNavController.view.layer addAnimation: transition forKey:nil];
    [secondNavController.view.layer addAnimation: transition forKey:nil];
    [thirdNavController.view.layer addAnimation: transition forKey:nil];
    
    self.tabBarController.viewControllers = [[NSArray alloc] initWithObjects: firstNavController, secondNavController, thirdNavController,  nil];
    self.tabBarController.delegate = self;
    self.tabBarController.selectedIndex = 1;
    
    HTAppDelegate*appDelegate = (HTAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    appDelegate.serivceImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, appDelegate.window.frame.size.height - 66, 108, 58)];
    appDelegate.serivceImg.image=[UIImage imageNamed: @"tabi_01.png"];
    
    appDelegate.contactImg=[[UIImageView alloc]initWithFrame:CGRectMake(108,  appDelegate.window.frame.size.height - 66, 107, 58)];
    appDelegate.contactImg.image=[UIImage imageNamed: @"tabi_02.png"];
    
    appDelegate.bookingImg=[[UIImageView alloc]initWithFrame:CGRectMake(215,  appDelegate.window.frame.size.height - 66, 105, 58)];
    
    appDelegate.bookingImg.image=[UIImage imageNamed: @"tabi_03.png"];
    [ self.tabBarController.view addSubview: appDelegate.serivceImg];
    [ self.tabBarController.view addSubview: appDelegate.contactImg];
    [ self.tabBarController.view addSubview: appDelegate.bookingImg];
    
    
    self.tabBarController.tabBar.frame = CGRectMake(0,  appDelegate.window.frame.size.height - 45, 320, 45);
    
    [self.view addSubview: self.tabBarController.view];
    

    完成这项工作的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

我会尝试使用不同的结构。首先,在app委托中创建标签栏控制器及其三个内容控制器,并使其成为窗口的根视图控制器。在第一个选项卡的控制器中,在viewDidAppearAnimated:方法中以模态方式显示您的启动页面 - 这将使该视图成为第一个可见的视图(如果您将动画设置为NO)。如果登录成功,只需关闭模态视图控制器,您就可以开始了。如果登录失败,请创建3个新的内容控制器,将其设置为选项卡栏控制器的viewControllers属性,然后关闭模式。这样,你只需要一个标签栏控制器,它就是根视图控制器,这是更常用的设置。