我在第一个控制器中创建了标签栏项目
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;
}
但在第二个控制器中
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"%d", chooseLevel);
}
chooseLevel 总是记录旧值。如果我按下第一个选项卡,然后按下第二个选项卡,则 chooseLevel 中的值是 1 而不是 2。
有谁知道如何解决它?