无法从AppDelegate自定义我的tabbar

时间:2013-02-21 04:05:51

标签: iphone ios uitabbarcontroller customization tabbar

我想为我的应用程序制作一个自定义tabbar,我已经尝试将我的代码放在第一个viewcontroller中,它可以工作! 但这是问题所在,我想声明并将我的代码放在Appdelegate.m中,但是当我运行它时,它只显示一个没有图像的黑色tabbar控制器。 为了您的信息,我从故事板中嵌入我的tabbar控制器。

这是代码

    UIImage *selectedImage0   = [UIImage imageNamed:@"img1.png"];
    UIImage *unselectedImage0 = [UIImage imageNamed:@"img1hov.png"];

    UIImage *selectedImage1   = [UIImage imageNamed:@"img2.png"];
    UIImage *unselectedImage1 = [UIImage imageNamed:@"img2hov.png"];

    UIImage *selectedImage2   = [UIImage imageNamed:@"img3.png"];
    UIImage *unselectedImage2 = [UIImage imageNamed:@"img3hov.png"];

    UIImage *selectedImage3   = [UIImage imageNamed:@"img4.png"];
    UIImage *unselectedImage3 = [UIImage imageNamed:@"img4hov.png"];

    UIImage *selectedImage4   = [UIImage imageNamed:@"img5.png"];
    UIImage *unselectedImage4 = [UIImage imageNamed:@"img5hov.png"];



    UITabBar *tabBar = self.tabBarController.tabBar;
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
    UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
    UITabBarItem *item4 = [tabBar.items objectAtIndex:4];

    tabBar.backgroundImage = [UIImage imageNamed:@"back_menu.png"];


    [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
    [item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
    [item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
    [item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
    [item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

该代码对我来说很好。如何在appDelegate中将UIViewController添加到UITabBarController?不是这样的吗?

//appDelegate.h
@property (nonatomic, strong) SomeViewController *viewController;
@property (nonatomic, strong) UITabBarController *tabBarController;
//and synthesize those

//appDelegate.m
self.viewController = [[SomeViewController alloc]  initWithNibName:@"SomeViewController" bundle:nil];

UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:self.viewController, nil];

您可以使用self.tabBarController

从任何地方访问appDelegate中的self.viewController((AppDelegate *)[UIApplication sharedApplication].delegate)