0

MainWindow.xibによって作成されたタブバーコントローラーがあります。4つのビューコントローラがあります。次に、5番目の項目をプログラムで追加したいと思います(コンパイル時までどのクラスを使用する必要があるかわからないため)

これは私のコードです:

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

どこが間違っているのですか?何か案は?

どうもありがとう

4

3 に答える 3

1

最後のタブがLoginUserViewControllerであり、UINavigationControllerのインスタンスではないことを忘れました。

LoginUserViewControllerをタブバーコントローラーに追加すると、タブバーコントローラーの配列内の最後のビューコントローラーはLoginUserViewControllerになり、UINavigationControllerのインスタンスではなくなります。

UINavigationController *navController = [tabBarController.viewControllers lastObject];

したがって、上記の行は、navController変数でLoginUserViewControllerのオブジェクトを返します。

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

したがって、LoginUserViewControllerにはviewControllersプロパティがないため、上記の行はクラッシュを引き起こします。

于 2013-02-15T13:21:51.847 に答える
1

これがすべてのコードである場合、ナビゲーションコントローラーをインスタンス化しているようには見えません。見る:

initWithRootViewController:

UINavigatorClassで。私は交換します:

UINavigationController *navController = [tabBarController.viewControllers lastObject];

と:

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

編集:もう1つの考え:「tabBarController」プロパティを合成した可能性があるため@synthesize tabBarController=tabBarController;
、@synthesizeを自動的に実行する最新バージョンのXCodeを使用することを強くお勧めします。コードの最後の前の行はself.tabBarControllerである必要があります

于 2013-02-15T13:22:13.073 に答える
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];
}
于 2013-02-15T13:21:40.833 に答える