0

私は iOS 開発の初心者で、これは非常に基本的な質問かもしれません。私のアプリではUITabBarController 、各 VC に 5 つの VC があります。タブバーアイテムのクリックを処理しています-

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    switch (item.tag)
    {
        case 0:
        {
            vc1 = [[VC1 alloc] initWithNibName:@"VC1" bundle:nil];
            [self.view addSubview:vc1.view];
            [tabbarObj setSelectedItem:[tabbarObj.items objectAtIndex:0]];
        }
            break;
        case 1:
        {
            vc2 = [[VC2 alloc] initWithNibName:@"VC2" bundle:nil];
            [self.view addSubview:vc2];
        }
            break;
        case 2:
        {
            vc3 = [[VC3 alloc] initWithNibName:@"VC3" bundle:nil];
            [self.view addSubview:vc3];
        }
            break;
        case 3:
        {
            [tabbarObj setSelectedItem:[tabbarObj.items objectAtIndex:3]];
        }
            break;
        case 4:
        {
            vc5 = [[VC5 alloc] initWithNibName:@"VC5" bundle:nil];
            [self.view addSubview:vc5];
        }
            break;
        default:
            break;
    }
}

タブバットアイテムをクリックするたびに現在のビューにサブビューが追加されるため、これはタブバーを処理する正しい方法ではないことを知っています。誰かが私にもっと良い方法を提案できますか?ありがとう。

4

1 に答える 1

3

Appedelegate.mファイル内メソッドを変更 しdidFinishLaunchingWithOptionsます

 tabBar_Controller = [[UITabBarController alloc] init];
    NSMutableArray *localControllersArray =[[NSMutableArray alloc]initWithCapacity:2];



    firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

    nav = [[UINavigationController alloc] initWithRootViewController:firstViewController];
nav.tabBarItem.title = @"item1";
    nav.navigationBar.barStyle = UIBarStyleBlack;
   [localControllersArray addObject:nav];
            [self setNav:nil];

    secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    nav.tabBarItem.title = @"item2";
           [localControllersArray addObject:nav];

    [self setNav:nil];
 tabBar_Controller.viewControllers = localControllersArray;
   tabBar_Controller.delegate = self;
    tabBar_Controller.selectedIndex = 0;
[self.window addSubview:tabBar_Controller.view];

ViewControllerを選択した後にいくつかのプロパティにアクセスするには、次のコードを使用します。

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewControllers
{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewControllers];

    switch (index) {


        case 0:
        {

            NSLog(@"selected 1");

            break;
        }
        case 1:

          {
            NSLog(@"selected 2");
                    break;
        }
        default:
            break;
    }


}

タブバーコントローラーの背景画像を変更するには、次のコードを使用できます。

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewControllers{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewControllers];

    switch (index) {
        case 0:
            [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_act21.png"]];

            break;
        case 1:
            [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_act22.png"]];
            break;
        case 2:
            [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_act23.png"]];
            break;
        default:
            break;
    }

    return YES;
}
于 2012-12-21T11:57:20.927 に答える