如何将Tab索引值从UITabBarController传递给UIViewController

问题描述 投票:0回答:5

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

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。

有谁知道如何解决它?

ios objective-c uitabbarcontroller
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;
}

FirstViewController.m

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

SecondViewController.m

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

OutPut:

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

2013-06-27 18:13:05.282样本标签[3981:c07] 1

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


0
投票

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

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


0
投票
You can use selected index property like this:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    chooseLevel = tabBarController.selected index;
}

0
投票
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    int selectedtag=self.tabBarController.selectedIndex;
    NSLog(@"%d", selectedtag);
}

0
投票

做这个 :

Rutavyukontrolarama

- (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);
}

FirstViewController

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

SecondViewController

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

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

谢谢,

© www.soinside.com 2019 - 2024. All rights reserved.