【问题标题】:Always display tabBarController when pushed to any viewController推送到任何 viewController 时始终显示 tabBarController
【发布时间】:2016-03-10 13:45:18
【问题描述】:

我有 1 个带有 3 个 ViewController 的 UITabbarController 属于其名为 aVC-bVC-cVC(VC = ViewController)。然后我有 1 个 MenuSideBar 和其他 3 个名为 dVC-eVC-fVC 的 ViewController。

我想要的只是每当我推送到 dVC 或 eVC 或 fVC 时,我的 UITabbarController 总是显示。我怎么能做到这一点?我真的停留在这一点上。我必须尝试很多方法,甚至自己自定义一个 tabbarController 但仍然无法正常工作。

请帮助我。这个案子我真的需要帮助。

非常感谢。

【问题讨论】:

  • 您只能通过在dVC-eVC-fVC 上添加一个 UITabBar 本身来做到这一点。在点击选项卡事件时,总是将根视图控制器更改为 nil 并再次将 UITabBarController 作为 rootview 控制器并打开作为通知传递的选定索引
  • @RajanMaheshwari 嗨,你的意思是我需要为每个要显示与 UITabbarController 一样的 Tabbar 的视图创建 UITabbar 吗?
  • 没错。实现这一目标的唯一方法
  • @RajanMaheshwari 嗨,你有关于这个案例的任何例子吗?我仍然不知道我应该做什么。请在这种情况下帮助我,我努力工作在这里呆了 3 天,但仍然无法做我需要的事情。
  • 好的,我会发布一个我将制作并上传到 github 上的示例,稍后会在答复中解释

标签: ios objective-c uitabbarcontroller swrevealviewcontroller


【解决方案1】:

我已经使用 SWRevealViewControllerObjective C 中为您制作了示例演示,正如您在问题中标记的那样。我使用的是 XCode 7.1.1 版本。确保在 7.x 版本的 XCode 中打开此演示。这个问题也可以通过MFSideMenu等其他侧边菜单库来解决

这里是我根据您的要求制作的demo示例的链接。
https://github.com/RajanMaheshwari/TabBarControllerApp

解释:

为了在您的应用程序中始终保留标签栏,您必须在每个控制器上添加一个 TabBar,而不是 UITabBarController 的添加视图控制器。在我的示例演示中,我采用了各种控制器

1.LeftSideViewController - 这将是整个应用程序的左侧面板。

2.MainTabBarViewController - 这将是UITabBarController 的子类,它将连接到其他两个视图控制器,这将是MainTabBarViewController 的一部分。
----a.TabFirstViewController
----b.TabSecondViewController

3.FirstViewController- 将来自 LeftSideViewController 的表视图并添加自己的UITabBar

4.SecondViewController- 将来自 LeftSideViewController 的 表视图并添加自己的UITabBar

导入 SWRevealViewController 文件夹。
在您的 AppDelegate 的 didFinishLaunchingWithOptions 方法中,我们需要将根视图控制器设为 MainTabBarViewController

AppDelegate.h

#import <UIKit/UIKit.h>
#import "SWRevealViewController.h"

@interface AppDelegate : UIResponder<UIApplicationDelegate,SWRevealViewControllerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) SWRevealViewController *viewController;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "LeftSideViewController.h"
#import "SWRevealViewController.h"
#import "MainTabBarViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

   //Here Adding some notification observers which will be fired whenvever a tabbar index in clicked of the viewcontrollers whose parent is not UITabBarController class and the left side menu ones too.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backToTabIndexControllerFirst) name:@"backToTabIndexControllerFirst" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backToTabIndexControllerSecond) name:@"backToTabIndexControllerSecond" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(firstVCWithoutNAV) name:@"firstVCWithoutNAV" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(secondVCWithoutNAV) name:@"secondVCWithoutNAV" object:nil];

    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window = window;
        UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:@"LeftSideViewController"];
        MainTabBarViewController *mainTabBarController = [storyBoard instantiateViewControllerWithIdentifier:@"MainTabBarViewController"];
        SWRevealViewController *mainRevealController =  [[SWRevealViewController alloc]
                                                    initWithRearViewController:leftSideController frontViewController:mainTabBarController];

    mainRevealController.delegate = self;
    self.viewController = mainRevealController;
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

添加了所有观察者的方法定义

AppDelegate.m

-(void)firstVCWithoutNAV{

    self.window.rootViewController = nil;
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UINavigationController * firstVC = [storyBoard instantiateViewControllerWithIdentifier:@"FirstNAV"];
    LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:@"LeftSideViewController"];
    SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                    initWithRearViewController:leftSideController frontViewController:firstVC];

    mainRevealController.delegate = self;

    self.viewController = mainRevealController;

    self.window.rootViewController = self.viewController;
}

-(void)secondVCWithoutNAV{

    self.window.rootViewController = nil;
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UINavigationController * secondVC = [storyBoard instantiateViewControllerWithIdentifier:@"SecondNAV"];
    LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:@"LeftSideViewController"];
    SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                    initWithRearViewController:leftSideController frontViewController:secondVC];

    mainRevealController.delegate = self;

    self.viewController = mainRevealController;

    self.window.rootViewController = self.viewController;

}

-(void)backToTabIndexControllerFirst{
    UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:@"LeftSideViewController"];


    MainTabBarViewController *mainTabBarController = [storyBoard instantiateViewControllerWithIdentifier:@"MainTabBarViewController"];

    SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                    initWithRearViewController:leftSideController frontViewController:mainTabBarController];

    mainRevealController.delegate = self;

    self.viewController = mainRevealController;

    self.window.rootViewController = self.viewController;
}

-(void)backToTabIndexControllerSecond{

    UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:@"LeftSideViewController"];


    MainTabBarViewController *mainTabBarController = [storyBoard instantiateViewControllerWithIdentifier:@"MainTabBarViewController"];

    //To make selected index as the one which is clicked from tab bar
    mainTabBarController.selectedIndex = 1;
    SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                    initWithRearViewController:leftSideController frontViewController:mainTabBarController];

    mainRevealController.delegate = self;

    self.viewController = mainRevealController;

    self.window.rootViewController = self.viewController;
}

在没有UITabBarController父类的Controller中,我们将添加一个tabBar并将tabBar的delegate设置为self,并添加tabBar的didSelectItem代理方法。

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController<UITabBarDelegate>
@property (weak, nonatomic) IBOutlet UITabBar *tabBar;

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SWRevealViewController.h"
@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    SWRevealViewController *revealController = [self revealViewController];

    [self.view addGestureRecognizer:revealController.panGestureRecognizer];


    UIBarButtonItem* revealButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"reveal-icon.png"] style:UIBarButtonItemStylePlain target:revealController action:@selector(revealToggle:)];

    self.navigationItem.leftBarButtonItem = revealButtonItem;

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSUInteger indexOfTab = [[tabBar items] indexOfObject:item];
    NSLog(@"Tab index = %u", (int)indexOfTab);

    if(indexOfTab == 0){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"backToTabIndexControllerFirst" object:nil];
    }else{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"backToTabIndexControllerSecond" object:nil];
    }
}

从此处触发的通知将更改应用程序的根控制器。

同样,我们将为 SecondViewController

编写代码 假设有一个 Table 用于从 LeftSidePanel 中选择其他各种控制器,我们将添加 tableView 的didSelectRowAtIndexPath 方法如下:

LeftSideViewController.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SWRevealViewController *revealController = self.revealViewController;
    [revealController revealToggleAnimated:YES];

    // Used this just to show the animation completion and then changing the root
    [self performSelector:@selector(changeControllers:) withObject:indexPath afterDelay:0.3];

}

-(void)changeControllers:(NSIndexPath*)indexPath{

    if(indexPath.row == 0){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"firstVCWithoutNAV" object:nil];
    }else{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"secondVCWithoutNAV" object:nil];
    }
}

我们还可以使用我已经为侧面板说明的其他库。示例
https://github.com/mikefrederick/MFSideMenu

希望这能解决问题!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多