0

我在第一个控制器中创建了标签栏项目

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。

有谁知道如何解决它?

4

5 回答 5

0

执行以下步骤:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
    viewController1.tabBarItem.tag = 0;
    UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
    viewController2.tabBarItem.tag = 1;
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = @[viewController1, viewController2];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

第一视图控制器.m

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"%d",self.tabBarItem.tag);
}

第二视图控制器.m

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"%d",self.tabBarItem.tag);
}

输出 :

2013-06-27 18:13:03.913 sampletabs[3981:c07] 0

2013-06-27 18:13:05.282 sampletabs[3981:c07] 1

谢谢,让我知道它对你有用吗?

于 2013-06-27T12:45:57.640 回答
0
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    int selectedtag=self.tabBarController.selectedIndex;
    NSLog(@"%d", selectedtag);
}
于 2013-06-27T12:03:15.010 回答
0
You can use selected index property like this:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    chooseLevel = tabBarController.selected index;
}
于 2013-06-27T12:02:35.507 回答
0

做这个 :

根视图控制器.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    tabBarController.delegate = self;
    tabBarController.view.frame = CGRectMake(0, 0, 320, 460);

    FirstViewController *stageViewRegular = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    [stageViewRegular setTitle:@"First"];
    stageViewRegular.tabBarItem.tag = 1;

    SecondViewController *stageViewAdvanced = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [stageViewAdvanced setTitle:@"Second"];
    stageViewAdvanced.tabBarItem.tag = 2;

    NSArray* controllersArray = [NSArray arrayWithObjects:stageViewRegular, stageViewAdvanced,  nil];
    tabBarController.viewControllers = controllersArray;

    tabBarController.selectedIndex = 0;
    [self.view addSubview:tabBarController.view];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"%d",viewController.tabBarItem.tag);
}

第一视图控制器

-(void)viewWillAppear:(BOOL)animated
{
    NSLog(@"viewWillAppear %d",self.tabBarItem.tag);
}

第二视图控制器

-(void)viewWillAppear:(BOOL)animated
{
    NSLog(@"viewWillAppear %d",self.tabBarItem.tag);
}

如果您需要选定选项卡索引的全局值,则在 AppDelegate 中创建 readWrite 属性并在 RootViewController 或 First/SecondViewController 中设置值。您将获得 ChooseLevel 的全局值。

谢谢,

于 2013-06-28T05:32:07.477 回答
0

为什么不使用 UITabBarController 的 selectedIndex 属性。我认为它具有您尝试使用的价值。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html

于 2013-06-27T11:46:47.740 回答