不显示tabBarController

时间:2012-12-13 08:55:19

标签: iphone ios uitabbarcontroller

我想以编程方式制作tabbarcontroller。页面中未显示tabBarController。任何人都可以说出错了。我们可以在应用程序中创建多个tabbarcontroller

ViewController.m
- (void)viewDidLoad
{
    report=[[UIViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    View1 *template=[[View1 alloc]initWithNibName:@"View1" bundle:nil];
    View2 *acc=[[View2 alloc]initWithNibName:@"View2" bundle:nil];
    View3 *four=[[View3 alloc]initWithNibName:@"View3" bundle:nil];
    View4 *five=[[View4 alloc]initWithNibName:@"View4" bundle:nil];   

    nav1=[[UINavigationController alloc]initWithRootViewController:report];
    nav2=[[UINavigationController alloc]initWithRootViewController:template];
    nav3=[[UINavigationController alloc]initWithRootViewController: acc];
    nav4=[[UINavigationController alloc]initWithRootViewController:four];
     nav5=[[UINavigationController alloc]initWithRootViewController:five];

    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Title" image:[UIImage  imageNamed:@"singleicon.png"] tag:0];
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"Reports" image:[UIImage imageNamed:@"doubleicon.png"] tag:1];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@" New " image:[UIImage imageNamed:@"clockicon.png"] tag:2];
    UITabBarItem *item3=[[UITabBarItem alloc]initWithTitle:@"four" image:[UIImage imageNamed:@"dependenticon.png"] tag:3];
    UITabBarItem *item4=[[UITabBarItem alloc]initWithTitle:@"five" image:[UIImage imageNamed:@"toolicon.png"] tag:4];

    nav1.tabBarItem = item;
    nav2.tabBarItem = item1;
    nav3.tabBarItem = item2;
    nav4.tabBarItem=item3;
    nav5.tabBarItem=item4;

    //[item1 setBadge:@"25"];
    self.tabBarController=[[UITabBarController alloc]init];
    [self.tabBarController setViewControllers:[NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5,nil]];

    self.report = self.tabBarController;
  // [self.report makeKeyAndVisible];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

5 个答案:

答案 0 :(得分:1)

使用以下代码:

     self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5,nil]];
     self.window.rootViewController = self.tabBarController;

<强>更新

也用于隐藏和显示UITabBar然后使用以下代码..

只需将此方法放在AppDelegate.m文件中,当您想要隐藏标签栏时,只需创建AppDelegate对象并调用以下hideTabBar方法

- (void) hideTabBar:(UITabBarController *) tabbarcontroller {

    int height = 480;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    for(UIView *view in tabbarcontroller.view.subviews) {
        if([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];
        } 
        else {
            [view setFrame:CGRectMake(view.frame.origin.x,view.frame.origin.y, 320, 436)];
        }
    }
    [UIView commitAnimations];
}

- (void) showTabBar:(UITabBarController *) tabbarcontroller {

    int height = 436;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; 

    for(UIView *view in tabbarcontroller.view.subviews) {

        if([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];            
        } 
        else {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, height)];
        }
    }    

    [UIView commitAnimations];
}

答案 1 :(得分:1)

将所有编码放在按钮的操作中并按下tabBarController,如下所示: -

[self.navigationController pushViewController:tabBarController animated:YES]; 

答案 2 :(得分:0)

如果您想以编程方式添加UITabBarController,则需要将tabbarcontroller添加到ViewController。你需要使用这一行,

[self.view addSubview:self.tabBarController.view];

答案 3 :(得分:0)

你需要在视图中添加tabBarController,你错过了一行

答案 4 :(得分:0)

我刚刚做了这个

[self.navigationController pushViewController:tabBarController animated:YES];