我正在尝试在我的IOS应用中创建自定义标签。我已经按照这个简单的指南http://kurrytran.blogspot.se/2011/10/ios-5-tutorial-creating-custom-tab-bar.html每个标签应该有2张背景图片,一张被选中,一张未被选中。我的问题是,在我的实际项目中,我的Tab栏控制器和视图控制器之间也有导航控制器,这似乎搞乱了一切。没有任何反应,我的所有标签都是黑色的,没有任何内容。我该怎么做才能使它发挥作用?
这是放在firstviewCotroller.m
中- (void)viewDidLoad
{
UIImage *selectedImage0 = [UIImage imageNamed:@"HomeDB.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:@"HomeLB.png"];
UIImage *selectedImage1 = [UIImage imageNamed:@"ScheduleDB.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:@"ScheduleLB.png"];
UIImage *selectedImage2 = [UIImage imageNamed:@"BuildingsDB.png"];
UIImage *unselectedImage2 = [UIImage imageNamed:@"BuildingsLB.png"];
UIImage *selectedImage3 = [UIImage imageNamed:@"InformationDB.png"];
UIImage *unselectedImage3 = [UIImage imageNamed:@"InformationLB.png"];
UIImage *selectedImage4 = [UIImage imageNamed:@"MoreDB.png"];
UIImage *unselectedImage4 = [UIImage imageNamed:@"MoreLB.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];
[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];
[super viewDidLoad];
}
答案 0 :(得分:0)
将UINavigationControllers分别放在MainWindow.xib中的每个标签中是不是更好,然后在你的App委托中使用上面的代码更改App Delegate中的标签栏项目。
我在App Delegate中的“didFinishLaunchingWithOptions”方法中执行以下操作。
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"tab-1-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-1.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"tab-2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-2.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"tab-3-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-3.png"]];
答案 1 :(得分:0)
Thanx为你的答案AndyDev!这是我的代码....
我的AppleDelegate.h文件:
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
UITabBarController *tabBarController;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
我的AppleDelegate.m文件:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize tabBarController;
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
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];
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"BuildingsLB.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"BuildingsLB.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"BuildingsLB.png"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:@"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"BuildingsLB.png"]];
[item4 setFinishedSelectedImage:[UIImage imageNamed:@"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"BuildingsLB.png"]];
return YES;
}
我可能做错了什么。在IOS之前,我从未尝试过定制任何东西,这是我在IOS开发的第一个项目。我是否还需要每个ViewController.m文件中的任何代码?这个代码至少没有任何事情发生....