简体   繁体   English

NSRangeException indexOutOfBound

[英]NSRangeException indexOutOfBound

I am using code that I made in another application. 我正在使用在另一个应用程序中编写的代码。 In the other application the code is working fine. 在另一个应用程序中,代码运行正常。 What I do is while loading splashViewController the images of slideShow are being downloaded and cached. 我要做的是在加载splashViewController时,正在下载并缓存slideShow的图像。 When the download is finished it will present the mainviewcontroller which is a maintableviewcontroller. 下载完成后,将显示mainviewcontroller,它是一个maintableviewcontroller。 Here is the code that I am using: 这是我正在使用的代码:

 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];

Please put a count check for tabbar.items first and then access all tab bar items : 请先对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
}

Also, since the tabbar is working fine in another application, please check your StoryBoard if it correctly creates and binds the tabbar for the rootViewController. 另外,由于该选项卡在另一个应用程序中可以正常工作,请检查您的StoryBoard是否正确创建并绑定了rootViewController的选项卡。

Here is a good post with detailed steps to create tabbar correctly using Storyboards which can help you debug/verify if you created the bindings and segues correctly: https://guides.codepath.com/ios/Using-Tab-Bar-Controllers 这是一篇不错的文章,其中包含使用情节提要正确创建标签栏的详细步骤,可以帮助您调试/验证是否正确创建了绑定和设置: https ://guides.codepath.com/ios/Using-Tab-Bar-Controllers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM