0

最初にアプリケーションについて説明します。私は6つのView Controllerを持っています

  • MenuViewController
  • FirstViewController
  • SecondViewController
  • サードビューコントローラー
  • FourthViewController
  • 五番目のViewController

これら 6 つの ViewController はすべて、navigationController にあります。アプリの起動時に、5 つのボタン (FirstViewController、SecondViewController、ThirdViewController、FourthViewController、FifthViewController の場合) を含むスプラッシュ画面の後、最初に「MenuViewController」を表示したいと考えています。この viewController では、ページの下部に TabBarController はありません。ただし、他のviewControllerはtabbarControllerになります。menuviewController の 5 つのボタンの 1 つに触れると、対応するビュー コントローラーが表示され、一番下に tabbarController があります。カスタムコーディング、ポジショニング、およびそれらの背後にある画像の設定でそれを行うことができます。コードは次のとおりです。

AppDelegate.h :

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
{
    UIImageView *firstTabImageView;
    UIImageView *secondTabImageView;
    UIImageView *thirdTabImageView;
    UIImageView *fourthTabImageView;
    UIImageView *fifthTabImageView;

    UIImage *firstTabImage;
    UIImage *firstTabActiveImage;

    UIImage *secondTabImage;
    UIImage *secondTabActiveImage;

    UIImage *thirdTabImage;
    UIImage *thirdTabActiveImage;

    UIImage *fourthTabImage;
    UIImage *fourthTabActiveImage;

    UIImage *fifthTabImage;
    UIImage *fifthTabActiveImage;
}

@property (strong, nonatomic) MenuViewController *menuViewController;
@property (strong, nonatomic) UINavigationController *navigationController;
@property (retain, nonatomic) IBOutlet UITabBarController *tabBarController;

AppDelegate.m :

-(void)makeTabBar
{
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.delegate=self;


    FirstViewController *firstViewController =[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    firstNavigationController.tabBarController.tabBar.tag = 0;


    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    secondNavigationController.tabBarController.tabBar.tag = 1;

    ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    UINavigationController *thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
    thirdNavigationController.tabBarController.tabBar.tag = 2;

    FourthViewController *fourthViewController = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil];
    UINavigationController *fourthNavigationController = [[UINavigationController alloc] initWithRootViewController:fourthViewController];
    fourthNavigationController.tabBarController.tabBar.tag = 3;

    FifthViewController *fifthViewController = [[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:nil];
    UINavigationController *fifthNavigationController = [[UINavigationController alloc] initWithRootViewController:fifthViewController];
    fifthNavigationController.tabBarController.tabBar.tag = 4;


    menuNavigationController.navigationBarHidden   = YES;
    firstNavigationController.navigationBarHidden  = YES;
    secondNavigationController.navigationBarHidden = YES;
    thirdNavigationController.navigationBarHidden  = YES;
    fourthNavigationController.navigationBarHidden = YES;
    fifthNavigationController.navigationBarHidden  = YES;

    NSArray *viewControllers =[[NSArray alloc]initWithObjects:
                           firstNavigationController,
                           secondNavigationController,
                           thirdNavigationController,
                           fourthNavigationController,
                           fifthNavigationController,
                           nil];

    firstTabImage        = [UIImage imageNamed:@"MenuTabImage.png"];
    firstTabActiveImage  = [UIImage imageNamed:@"MenuTabImage_Active.png"];

    secondTabImage       = [UIImage imageNamed:@"TVGuideTabImage.png"];
    secondTabActiveImage = [UIImage imageNamed:@"TVGuideTabImage_Active.png"];

    thirdTabImage        = [UIImage imageNamed:@"FirstRankTabImage.png"];
    thirdTabActiveImage  = [UIImage imageNamed:@"FirstRankTabImage_Active.png"];

    fourthTabImage       = [UIImage imageNamed:@"FavoriteTabImage.png"];
    fourthTabActiveImage = [UIImage imageNamed:@"FavoriteTabImage_Active.png"];

    fifthTabImage        = [UIImage imageNamed:@"RegistrationTabImage.png"];
    fifthTabActiveImage  = [UIImage imageNamed:@"RegistrationTabImage_Active.png"];


    CGRect frame  = CGRectMake(0, 0, 320, 52);
    UIView *viewa = [[UIView alloc] initWithFrame:frame];
    UIImage *tabBarBackgroundImage = [UIImage imageNamed:@"Tab_Back.png"];
    UIColor *color = [[UIColor alloc] initWithPatternImage:tabBarBackgroundImage];
    [viewa setBackgroundColor:color];

    if(IS_IPHONE_5)
    {
        [[self.tabBarController tabBar] insertSubview:viewa atIndex:1];
        firstTabImageView  = [[UIImageView alloc]initWithFrame:CGRectMake(0, 519, 64, 49)];
        secondTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(64, 519,64, 49)];
        thirdTabImageView  = [[UIImageView alloc]initWithFrame:CGRectMake(128, 519,64, 49)];
        fourthTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(192, 519, 64, 49)];
        fifthTabImageView  = [[UIImageView alloc]initWithFrame:CGRectMake(256, 519, 64, 49)];
    }
    else
    {
        [[self.tabBarController tabBar] insertSubview:viewa atIndex:0];
        firstTabImageView  = [[UIImageView alloc]initWithFrame:CGRectMake(0, 431, 64, 49)];
        secondTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(64, 431,64, 49)];
        thirdTabImageView  = [[UIImageView alloc]initWithFrame:CGRectMake(128, 431,64, 49)];
        fourthTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(192, 431, 64, 49)];
        fifthTabImageView  = [[UIImageView alloc]initWithFrame:CGRectMake(256, 431, 64, 49)];
    }

    firstTabImageView.image  = firstTabActiveImage;
    secondTabImageView.image = secondTabImage;
    thirdTabImageView.image  = thirdTabImage;
    fourthTabImageView.image = fourthTabImage;
    fifthTabImageView.image  = fifthTabImage;

    [self.tabBarController.view addSubview:firstTabImageView];
    [self.tabBarController.view addSubview:secondTabImageView];
    [self.tabBarController.view addSubview:thirdTabImageView];
    [self.tabBarController.view addSubview:fourthTabImageView];
    [self.tabBarController.view addSubview:fifthTabImageView];
    self.tabBarController.delegate=self;
    self.tabBarController.selectedIndex=0;

    [tabBarController setViewControllers:viewControllers animated:NO];
}

- (void)tabBarController:(UITabBarController *)tabBarController1 didSelectViewController:(UIViewController *)viewController1
{
    if (viewController1 == [tabBarController1.viewControllers objectAtIndex:0])
    {
        firstTabImageView.image  = [UIImage imageNamed:@"MenuTabImage_Active.png"];
        secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage.png"];
        thirdTabImageView.image  = [UIImage imageNamed:@"FirstRankTabImage.png"];
        fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage.png"];
        fifthTabImageView.image  = [UIImage imageNamed:@"RegistrationTabImage.png"];
    }
    else if (viewController1 == [tabBarController1.viewControllers objectAtIndex:1])
    {
        firstTabImageView.image  = [UIImage imageNamed:@"MenuTabImage.png"];
        secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage_Active.png"];
        thirdTabImageView.image  = [UIImage imageNamed:@"FirstRankTabImage.png"];
        fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage.png"];
        fifthTabImageView.image  = [UIImage imageNamed:@"RegistrationTabImage.png"];
    }
    else if (viewController1 == [tabBarController1.viewControllers objectAtIndex:2])
    {
        firstTabImageView.image  = [UIImage imageNamed:@"MenuTabImage.png"];
        secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage.png"];
        thirdTabImageView.image  = [UIImage imageNamed:@"FirstRankTabImage_Active.png"];
        fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage.png"];
        fifthTabImageView.image  = [UIImage imageNamed:@"RegistrationTabImage.png"];
    }
    else if (viewController1 == [tabBarController1.viewControllers objectAtIndex:3])
    {
        firstTabImageView.image  = [UIImage imageNamed:@"MenuTabImage.png"];
        secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage.png"];
        thirdTabImageView.image  = [UIImage imageNamed:@"FirstRankTabImage.png"];
        fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage_Active.png"];
        fifthTabImageView.image  = [UIImage imageNamed:@"RegistrationTabImage.png"];
    }
    else if (viewController1 == [tabBarController1.viewControllers objectAtIndex:4])
    {
        firstTabImageView.image  = [UIImage imageNamed:@"MenuTabImage.png"];
        secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage.png"];
        thirdTabImageView.image  = [UIImage imageNamed:@"FirstRankTabImage.png"];
        fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage.png"];
        fifthTabImageView.image  = [UIImage imageNamed:@"RegistrationTabImage_Active.png"];
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self makeTabBar];

    MenuViewController *menuViewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
    navigationController = [[UINavigationController alloc] initWithRootViewController:menuViewController];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

異なるインデックスを持つすべての「IBAction」にこのコードを使用します:例:「インデックス:0」の「FirstViewController」

- (IBAction)GotoFirstViewController:(id)sender
{
    AppDelegate *appdelegte = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [[[appdelegte navigationController] view]removeFromSuperview];
    [[appdelegte window] addSubview:[[appdelegte tabBarController] view]];
    [[appdelegte tabBarController] setSelectedIndex:0];
}

問題は、ViewController に移動した後で、「MenuViewController」に戻ること です。次のコードを使用します。

-(IBAction)goBack:(id)sender
{
    menuViewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
    [self.navigationController pushViewController:menuViewController animated:YES];
}

同じボタンを押して別の ViewController (5 つのいずれか) に移動できません。 もし誰かが知っていれば、タブバーが「secondViewController(ここでは:firstViewController)」からセットアップされたときに、他のViewControllerから「firstViewController(ここでは:menuViewController)」に来た後、タブバーに接続されている他のViewControllerで再び行く方法、私と共有してください。この投稿をお読みいただき、ありがとうございます。- トゥーロン

4

3 に答える 3

1
- (void)addToolBarToView:(UIToolbar *)rootToolBar currentView:(UIViewController *)currentView{

CGRect rect = currentView.view.frame;

rootToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, rect.size.height-44, rect.size.width, 44)];

[rootToolBar setBarStyle:UIBarStyleDefault];

[rootToolBar sizeToFit];

UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_ricerca.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showSearch)];

UIBarButtonItem *cartBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_shopping_cart.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showCart)];

UIBarButtonItem *settingBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_settings.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showSettings)];

UIBarButtonItem *favoriteBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_preferiti.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showFavorites)];

UIBarButtonItem *savedBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_save.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showSaved)];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

NSArray *barItems = [NSArray arrayWithObjects:searchBtn, flexSpace, cartBtn, flexSpace, favoriteBtn, flexSpace, settingBtn, flexSpace, savedBtn ,nil];

[rootToolBar setItems:barItems animated:YES];

[currentView.view addSubview:rootToolBar];
}
于 2013-11-07T15:35:42.033 に答える
0

このコードを AppDelegate.m に追加します

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    self.tabBarController.selectedViewController=viewController;
    return YES;
}
于 2013-09-12T07:55:30.727 に答える