应用程序崩溃开始

时间:2012-05-21 05:21:14

标签: objective-c ios xcode

您好我之前正在做一些测试,我的应用运行得很好。我想做更多测试,所以我决定从我的设备中删除该应用程序,然后通过运行重新安装。

现在好了,出于某种原因,我进入了我的启动画面出现然后它崩溃的阶段,我收到了错误:

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

1 个答案:

答案 0 :(得分: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];