2012-05-21 230 views
-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]; 
} 
+0

請張貼代碼 –

+0

顯示爲NSArray的和相關的循環的代碼。 – Sarah

+0

@ColeJohnson以大量代碼發佈......大型應用程序。有沒有通過Xcode調試發生這種情況?謝謝 –

回答

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

對不起,我已經發布舊代碼,我做了這個改變,我仍然有錯誤。我在某處看到,出於某種奇怪的原因,有時ios5不喜歡for循環,並且爲每個循環使用類似a的東西(我認爲這就是它所稱的)會起作用。你知道這是怎麼完成的嗎?謝謝 –

+0

解決了這個問題和更簡潔的代碼。謝謝你的幫助! –