2

我有一個Storyboard Xcode項目,我正在努力將選項卡的順序放置在Tabbar中。其中有4個選項卡。以編程方式更改TabBar順序目標C

CurrentLocationViewController, LocationsViewController, MapViewController and AnimalsViewController 

我有一個Sqlite數據庫連接我的項目中的我的選項卡3。位置,標籤和地圖。

然後我有一個選項卡,它與我的項目中的其他3個選項卡完全分開。

我想AnimalsViewController或動物選項卡是我的第一個選項卡在我的應用程序。我可以添加它作爲我的第四個標籤欄項沒有問題,但當我更改順序爲我的界面生成器中的第一個,我的應用程序崩潰。

我可以很容易地改變標籤,位置和地圖的順序,但是當我在開始時引入Animal標籤時,它會崩潰。

標籤,位置和地圖選項卡相互連接,動物選項卡與它完全分離。

我知道我必須在我的代碼中引用下面的代碼中的選項卡,它位於我的AppDelegate中,幷包含AnimalsViewController.h,但我試圖無效。

下面是我的代碼,圖片和錯誤日誌。

2013-04-08 16:38:08.075 ShotPlacementGuide[5077:c07] -[AnimalsViewController viewControllers]: unrecognized selector sent to instance 0xae8dc90 
    2013-04-08 16:38:08.077 ShotPlacementGuide[5077:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AnimalsViewController viewControllers]: unrecognized selector sent to instance 0xae8dc90' 
    *** First throw call stack: 
    (0x24a9012 0x1c5ce7e 0x25344bd 0x2498bbc 0x249894e 0x2443 0xb01157 0xb01747 0xb0294b 0xb13cb5 0xb14beb 0xb06698 0x1fc9df9 0x1fc9ad0 0x241ebf5 0x241e962 0x244fbb6 0x244ef44 0x244ee1b 0xb0217a 0xb03ffc 0x222d 0x2155) 
    libc++abi.dylib: terminate called throwing an exception 
    (lldb) 

這裏是我的代碼:

UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController; 

    UINavigationController *navigationController = (UINavigationController *) [[tabBarController viewControllers] objectAtIndex:0]; 

    CurrentLocationViewController *currentLocationViewController = (CurrentLocationViewController *) [[navigationController viewControllers] objectAtIndex:0]; 
    currentLocationViewController.managedObjectContext = self.managedObjectContext; 

    navigationController = (UINavigationController *) [[tabBarController viewControllers] objectAtIndex:1]; 

    LocationsViewController *locationsViewController = (LocationsViewController *) [[navigationController viewControllers] objectAtIndex:0]; 
    locationsViewController.managedObjectContext = self.managedObjectContext; 

    MapViewController *mapViewController = (MapViewController *) [[tabBarController viewControllers] objectAtIndex:2]; 

    mapViewController.managedObjectContext = self.managedObjectContext; 

enter image description here

enter image description here

回答

1

爲了在你的標籤欄控制器用來交換的第一和第四視圖控制器驗證碼:

UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;  
NSMutableArray* viewControllers = [[tabBarController viewControllers] mutableCopy];  
[viewControllers exchangeObjectAtIndex:0 withObjectAtIndex:3];  
[tabBarController setViewControllers:viewControllers]; 
相關問題