我可以设置一次背景,但之后它就再也不会改变了。我在stackoverflow上看过所有的例子。代码示例看起来总是一样的。我已经设置了代表。图像一切正常。我已将它们一个接一个地设置为默认图像,并将显示。但是在应用程序完成启动后,后台不再发生任何事情。
这是我的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self customizeInterface];
// Override point for customization after application launch.
self.tabController = (UITabBarController *)self.window.rootViewController;
self.tabController.delegate = self;
...
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
int tabitem = self.tabController.selectedIndex;
NSLog(@"tabitem: %i", tabitem);
[self switchTabBarImage:tabitem];
//[[tabController objectAtIndex:tabitem] popToRootViewControllerAnimated:YES];
}
- (void)switchTabBarImage:(int)selectedIndex
{
NSLog(@"selected: %i", selectedIndex);
if (selectedIndex == 0) {
NSLog(@"0");
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-1.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
if (selectedIndex == 1) {
NSLog(@"1");
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-2.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
if (selectedIndex == 2) {
NSLog(@"2");
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-3.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
if (selectedIndex == 3) {
NSLog(@"3");
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-4.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
}
- (void)customizeInterface
{
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-1.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
UIImage *selectionIndicator = [UIImage imageNamed:@"tabbar-icon-clean.png"];
[[UITabBar appearance] setSelectionIndicatorImage:selectionIndicator];
}
调试器显示:
2012-11-13 02:42:06.147 soundapp[9060:c07] tabitem: 1
2012-11-13 02:42:06.148 soundapp[9060:c07] selected: 1
2012-11-13 02:42:06.148 soundapp[9060:c07] 1
2012-11-13 02:42:07.739 soundapp[9060:c07] tabitem: 2
2012-11-13 02:42:07.739 soundapp[9060:c07] selected: 2
2012-11-13 02:42:07.740 soundapp[9060:c07] 2
我正在寻找几个小时,但无法弄清楚为什么它只工作一次。有人在我的代码中看到错误吗?