0

我有一個由MainWindow.xib創建的選項卡欄控制器。我有4個視圖控制器。現在我想以編程方式添加第五項(因爲我不知道我將不得不使用,直到編譯時哪一類)將新的viewcontroller添加到選項卡控制器

這是我的代碼:

UIViewController * login = [[LoginUserViewController alloc] initWithNibName:@"LoginUserViewController" bundle:nil]; 

NSMutableArray * viewControllersArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers]; 
[viewControllersArray addObject:login]; 
[self.tabBarController setViewControllers:viewControllersArray 
           animated:YES]; 

,但我得到

[LoginUserViewController viewControllers]: unrecognized selector sent to instance 0x95791b0' 

當我到了這個代碼

UINavigationController *navController = [tabBarController.viewControllers lastObject]; 
LoginViewController * log = [navController.viewControllers objectAtIndex:0]; 

我要去哪裏錯了?有任何想法嗎?

非常感謝

回答

0

試試這個....

- (void) setUpTabBar { 
FirstViewController *firstViewController = [[FirstViewController alloc]init]; 
firstViewController.title = @"First View"; 
firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0]; 
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController]; 

SecondViewController *secondViewController = [[SecondViewController alloc]init]; 
secondViewController.title = @"Second View"; 
secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1]; 
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController]; 

ThirdViewController *thirdViewController = [[ThirdViewController alloc]init]; 
thirdViewController.title = @"Third View"; 
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2]; 
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController]; 

ForthViewController *forthViewController = [[ForthViewController alloc]init]; 
forthViewController.title = @"Forth View"; 
forthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2]; 
UINavigationController *forthNavController = [[UINavigationController alloc]initWithRootViewController:forthViewController]; 

tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil]; 
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, forthNavController, nil]; 
tabBarController.delegate = self;    
[self sizeViewToAvailableWindow:[tabBarController view]]; 

[firstNavController release]; 
[firstViewController release]; 

[secondNavController release]; 
[secondViewController release]; 

[thirdNavController release]; 
[thirdViewController release]; 

[forthNavController release]; 
[forthViewController release]; 
} 
+0

這就是我所需要的,實例化一個UINavigationController – pdrcabrod 2013-02-18 08:24:25

1

你忘了,最後一個選項卡是LoginUserViewController而不是UINavigationController的一個實例。

添加LoginUserViewController到標籤欄控制器之後,在標籤欄控制器的陣列的最後一個視圖控制器將LoginUserViewController且不的UINavigationController

的實例
UINavigationController *navController = [tabBarController.viewControllers lastObject]; 

因此上述線將返回LoginUserViewController的對象在navController變量。

RecordsViewController *recordsViewController = [navController.viewControllers objectAtIndex:0]; 

因此上述線路將引起死機的LoginUserViewController沒有viewControllers財產。

1

如果這是您的所有代碼,它看起來不像您正在實例化導航控制器。看看:

initWithRootViewController: 

在UINavigatorClass。我將取代:

UINavigationController *navController = [tabBarController.viewControllers lastObject]; 

有:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: [tabBarController.viewControllers lastObject]]; 

編輯: 還有一個想法: 看起來你對「tabBarController」屬性運氣好,因爲你可能已經合成了它作爲@synthesize tabBarController=tabBarController;
我強烈建議您使用最新版本的XCode,它會自動爲您執行@synthesize。在你的代碼中的最後一行應該是self.tabBarController

相關問題