-2

嗨,我早些时候做了一些测试,我的应用程序运行得很好。我想做更多的测试,所以我决定从我的设备中删除该应用程序,然后通过运行重新安装它。

好吧,现在由于某种原因,我进入了我的启动屏幕出现的阶段,然后它崩溃了,我得到了错误:

 Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'

这显然意味着数组越界正确吗?但是为什么现在以及我如何才能知道这发生在哪里和哪个视图控制器上?当我尝试通过再次运行它来重新安装应用程序时,它怎么会突然运行,我得到这个错误?

谢谢

编辑 错误与以下代码中的数组有关

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

  exploreViewController *view1 = [[exploreViewController alloc] initWithNibName:@"exploreViewController" bundle:nil];
view1.title= @"Explore";

Upcoming *view2 = [[Upcoming alloc] initWithNibName:@"Upcoming" bundle:nil];
view2.title = @"Upcoming";

calcViewController *view3 = [[calcViewController alloc] initWithNibName:@"calcViewController" bundle:nil];
view3.title = @"Calc";

UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:view1];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:view2];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:view3];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nil];
self.tabBarItem = [[[UITabBarItem alloc] init] autorelease];

NSArray *tabBarItems = self.tabBarController.tabBar.items; 
UIImage *tab1 = [UIImage imageNamed:@"85-trophy.png"]; 
UIImage *tab2 = [UIImage imageNamed:@"12-eye.png"]; 
UIImage *tab3 = [UIImage imageNamed:@"237-key.png"];

NSArray *tabBarImages = [[[NSArray alloc] initWithObjects:tab1, tab2, tab3,nil]autorelease]; 
NSInteger tabBarItemCounter; 
for (tabBarItemCounter = 0; tabBarItemCounter < [tabBarItems count]; tabBarItemCounter++) 
{ 
    tabBarItem = [tabBarItems objectAtIndex:tabBarItemCounter]; 
    tabBarItem.image = [tabBarImages objectAtIndex:tabBarItemCounter]; 
} 
4

1 回答 1

2

好吧,这次崩溃的原因如下:您正在向 tabBar 添加五个项目(nav1、nav2、nav3、nav4、nav6),但您的选项卡只有三个图像(tab1、tab2、tab3)。因此,当 for 循环到达第四个选项卡时,它会崩溃,因为tabBarImages它只包含三个对象。

除此之外,您的代码看起来有点乱 - 这可能是第一眼看不到错误的原因。

// 编辑

您使事情复杂化了-只需尝试以下代码

UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav1.title = @"Explore";
nav1.tabBarItem.image = [UIImage imageNamed:@"85-trophy.png"];

UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav2.title = @"Upcoming";
nav2.tabBarItem.image = [UIImage imageNamed:@"12-eye.png"];

UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav3.title = @"Calc";
nav3.tabBarItem.image = [UIImage imageNamed:@"237-key.png"];

UITabBarController *tabBarController = [[UITabBarController alloc] init];    
[tabBarController setViewControllers:[NSArray arrayWithObjects:nav1, nav2, nav3, nil]];

[nav1 release];
[nav2 release];
[nav3 release];
于 2012-05-21T06:37:12.593 回答