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

しかし、2番目のコントローラーで

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

    NSLog(@"%d", chooseLevel);
}

chooseLevel は常に古い値を記録します。最初のタブを押してから 2 番目のタブを押すと、chooseLevel の値は 2 ではなく 1 になります。

誰もそれを解決する方法を知っていますか?

4

5 に答える 5

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

次の手順を実行します。

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

出力:

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

UITabBarController の selectedIndex プロパティを使用しないのはなぜですか。あなたが使おうとしている価値があると思います。

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

于 2013-06-27T11:46:47.740 に答える
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 に答える