最初のコントローラーでタブバー項目を作成しました
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
StageViewController *stageViewRegular = [[StageViewController alloc] init];
[stageViewRegular setTitle:@"Regular"];
stageViewRegular.tabBarItem.tag = 1;
StageViewController *stageViewAdvanced = [[StageViewController alloc] init];
[stageViewAdvanced setTitle:@"Advanced"];
stageViewAdvanced.tabBarItem.tag = 2;
NSArray* controllersArray = [NSArray arrayWithObjects:stageViewRegular, stageViewAdvanced, nil];
tabBarController.viewControllers = controllersArray;
tabBarController.selectedIndex = 0;
[self.view addSubview:tabBarController.view];
そして、上記のように tabBarItem.tag をコントローラーに渡したい
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
chooseLevel = viewController.tabBarItem.tag;
}
しかし、2番目のコントローラーで
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"%d", chooseLevel);
}
chooseLevel は常に古い値を記録します。最初のタブを押してから 2 番目のタブを押すと、chooseLevel の値は 2 ではなく 1 になります。
誰もそれを解決する方法を知っていますか?