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

时间:2013-09-12 07:02:56

标签: objective-c uitabbar uinavigation

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

  • MenuViewController
  • FirstViewController
  • SecondViewController
  • ThirdViewController
  • FourthViewController
  • FifthViewController

所有这6个ViewControllers都在navigationController中。我想首先在app的开始时间显示我的“MenuViewController”,在启动画面之后,它包含5个按钮(For: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”: 例如:带有“index: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(五个中的任何一个)。 如果有人知道,当tabbar从“seceondViewController(这里:firstViewController)”设置后,然后从其他ViewController进入“firstViewController(这里:menuViewController)”之后,如何再次使用与tabbar连接的其他ViewController,请与我分享。 非常感谢阅读这篇文章,并提前多多感谢。 - Tulon

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

在AppDelegate.m

添加此代码
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    self.tabBarController.selectedViewController=viewController;
    return YES;
}

答案 2 :(得分:0)

基于Apple文档,UITabBar是UIViewController的子类,可以是子视图..!但它必须是UIWindow rootController,如果不是你的应用程序将崩溃!我最后遇到了同样的问题我不得不使用UIToolBar来做同样的事情。虽然在定制方面做了很多努力。