首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSRangeException indexOutOfBound

NSRangeException indexOutOfBound
EN

Stack Overflow用户
提问于 2016-12-14 14:52:15
回答 1查看 67关注 0票数 0

我使用的是我在另一个应用程序中编写的代码。在另一个应用程序中,代码运行良好。我所做的是,当加载splashViewController时,slideShow的图像正在被下载和缓存。下载完成后,它将显示mainviewcontroller,这是一个可维护的视图控制器。下面是我使用的代码:

代码语言:javascript
复制
 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];
EN

回答 1

Stack Overflow用户

发布于 2016-12-16 01:42:58

请先对tabbar.items进行计数检查,然后访问所有选项卡栏项目:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41136466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档