应用程序在启动时崩溃
您好我之前正在做一些测试,我的应用程序运行得很好。我想做一些更多的测试,所以我决定从我的设备中删除应用程序,然后通过运行重新安装。应用程序在启动时崩溃
现在好了,由于某种原因,我得到的地方,我的启动画面显示搭台,然后它崩溃,我得到的错误:
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];
}
嗯,这个崩溃的原因数组如下: 你加入五项您的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];
对不起,我已经发布旧代码,我做了这个改变,我仍然有错误。我在某处看到,出于某种奇怪的原因,有时ios5不喜欢for循环,并且为每个循环使用类似a的东西(我认为这就是它所称的)会起作用。你知道这是怎么完成的吗?谢谢 –
解决了这个问题和更简洁的代码。谢谢你的帮助! –
请张贴代码 –
显示为NSArray的和相关的循环的代码。 – Sarah
@ColeJohnson以大量代码发布......大型应用程序。有没有通过Xcode调试发生这种情况?谢谢 –