来自第二个视图控制器的 UItabBar 但所有视图控制器都有 UInavigation 并返回到 firstViewController 问题

Posted

技术标签:

【中文标题】来自第二个视图控制器的 UItabBar 但所有视图控制器都有 UInavigation 并返回到 firstViewController 问题【英文标题】:UItabBar from second view controller but all viewController have UInavigation with back to firstViewController issue 【发布时间】:2013-09-12 07:02:56 【问题描述】:

让我先解释一下我的申请。我有六个视图控制器

菜单视图控制器 FirstViewController SecondViewController 第三视图控制器 FourthViewController FifthViewController

所有这 6 个 ViewController 都在 navigationController 中。我想在应用程序启动时首先显示我的“MenuViewController”,在初始屏幕之后,其中包含 5 个按钮(对于:FirstViewController、SecondViewController、ThirdViewController、FourthViewController、FifthViewController)。在这个 viewController 中,页面底部不会有 TabBarController。但其他 viewController 将在 tabbarController 中。当我在 menuviewController 中触摸这五个按钮之一时,它会将我带到相应的视图控制器,在那里我可以在底部找到 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”: 例如:“FirstViewController”与“index:0”

- (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(五个中的任何一个)。 如果有人知道,当从“seceondViewController(此处:firstViewController)”设置标签栏时,从其他ViewController进入“firstViewController(此处:menuViewController)”后,如何再次进入与标签栏相连的其他ViewController,请与我分享。 非常感谢您阅读这篇文章,并在此先感谢您。 - 图伦

【问题讨论】:

【参考方案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];

【讨论】:

谢谢伙计,我会试试的。 :)【参考方案2】:

在您的 AppDelegate.m 中添加此代码

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

    self.tabBarController.selectedViewController=viewController;
    return YES;

【讨论】:

萨拉姆,这不是工作的兄弟。我想,我必须在“GotoFirstViewController”按钮操作中添加一些东西。【参考方案3】:

根据苹果文档,UITabBar 是 UIViewController 的子类,可以是子 View..!但它必须是 UIWindow rootController 如果不是你的应用程序会崩溃!!!最后我遇到了同样的问题,我不得不使用 UIToolBar 来做同样的事情。虽然在定制方面非常努力。

【讨论】:

以上是关于来自第二个视图控制器的 UItabBar 但所有视图控制器都有 UInavigation 并返回到 firstViewController 问题的主要内容,如果未能解决你的问题,请参考以下文章

如果在视图控制器中使用 Tabbar,如何设置主视图控制器

UITabBar 自定义背景图像应用于一个视图控制器而不是 AppDelegate

第二个选项卡的 UITabbar 项目标题/名称消失

如何将UITabBar嵌入故事板xcode中的另一个UITabBar

导航返回时导航控制器和视图未更新

使用两个视图控制器,第二个不会显示视图