我使用的是我在另一个应用程序中编写的代码。在另一个应用程序中,代码运行良好。我所做的是,当加载splashViewController时,slideShow的图像正在被下载和缓存。下载完成后,它将显示mainviewcontroller,这是一个可维护的视图控制器。下面是我使用的代码:
UIImage* tabBarBackground = [UIImage imageNamed:@"tabBar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
UITabBarController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"barController"];
UITabBar *tabBar = rootViewController.tabBar;
UITabBarItem *tabBarItem1 = tabBar.items[0];// the error here happening event if i comment out this line the error is hapen at the next uibar items
UIImage *selectedLogo = [[UIImage imageNamed:@"productSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedLogo = [[UIImage imageNamed:@"product"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem1 setImage:unselectedLogo];
[tabBarItem1 setSelectedImage:selectedLogo];
[tabBarItem1 setTitle:@"My Product"];
UITabBarItem *tabBarItem2 = tabBar.items[1];
UIImage *selectednews = [[UIImage imageNamed:@"notificationSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectednews = [[UIImage imageNamed:@"notification"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem2 setImage:unselectednews];
[tabBarItem2 setSelectedImage:selectednews];
[tabBarItem2 setTitle:@"Notifications"];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UIImage *selectedLocation = [[UIImage imageNamed:@"locationSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedLocation = [[UIImage imageNamed:@"location"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem3 setImage:unselectedLocation];
[tabBarItem3 setSelectedImage:selectedLocation];
[tabBarItem3 setTitle:@"Locate Us"];
UITabBarItem *tabBarItem4 = tabBar.items[3];
UIImage *selectedaboutus = [[UIImage imageNamed:@"moreSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedaboutus = [[UIImage imageNamed:@"more"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem4 setImage:unselectedaboutus];
[tabBarItem4 setSelectedImage:selectedaboutus];
[tabBarItem4 setTitle:@"More"];
[self.loadingActivityIndicator stopAnimating];
[self.navigationController popToRootViewControllerAnimated:YES];
[self presentViewController:rootViewController animated:YES completion:nil];发布于 2016-12-16 01:42:58
请先对tabbar.items进行计数检查,然后访问所有选项卡栏项目:
if(tabbar.items.count==tabCount) //tabCount is number of tabs in your tabbar
{
UITabBarItem *tabBarItem1 = tabBar.items[0];
//<remaining code>
}else
{
//either tabbar is nil or tabbar doesn't not have any tabs, so handle accordingly
}此外,由于the栏在另一个应用程序中工作正常,请检查您的StoryBoard是否正确地创建并绑定了rootViewController的the栏。
这里有一个很好的帖子,详细介绍了使用故事板正确创建选项卡栏的步骤,这些步骤可以帮助您调试/验证是否正确地创建了绑定和segues:https://guides.codepath.com/ios/Using-Tab-Bar-Controllers
https://stackoverflow.com/questions/41136466
复制相似问题