推送到任何 viewController 时始终显示 tabBarController
Always display tabBarController when pushed to any viewController
我有 1 个 UITabbarController,其中 3 个 ViewControllers 属于其命名的 aVC-bVC-cVC(VC = ViewController).然后我有 1 个 MenuSideBar 和其他 3 个 ViewControllers 命名为 dVC-eVC-fVC。
我想要的只是每当我按下 dVC 或 eVC 或 fVC 时,我的 UITabbarController 总是显示。我该怎么做?我真的卡在了这一点上。我必须尝试很多方法,甚至自己自定义 tabbarController 但仍然无法正常工作。
请帮帮我。在这个案子上我真的需要帮助。
非常感谢。
我在 Objective C 中使用 SWRevealViewController 为您制作了示例演示,正如您在问题中标记的那样。我用的是XCode7.1.1版本。确保在 XCode 的 7.x 版本中打开此演示。这个问题也可以通过其他侧边菜单库解决,比如 MFSideMenu 等
这是我根据您的要求制作的demo样例link
https://github.com/RajanMaheshwari/TabBarControllerApp
解释:
为了在您的应用程序中始终保留标签栏,您必须在每个控制器上添加一个 TabBar,这不是 UITabBarController
的 viewController 添加。在我的示例演示中,我采用了各种控制器
1.LeftSideViewController
- 这将是整个应用程序的左侧面板。
2.MainTabBarViewController
- 这将是 UITabBarController
subclass 并且它将连接到另外两个视图控制器成为 MainTabBarViewController
.
的一部分
----a.TabFirstViewController
----b.TabSecondViewController
3.FirstViewController
- 这将是来自 LeftSideViewController 的 table 视图并具有它自己的 UITabBar
添加。
4.SecondViewController
- 这将是来自 LeftSideViewController 的 table 视图并具有它自己的 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 parent class 的控制器中,我们将添加一个 tabBar 并将 tabBar 的委托设置为 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
希望这能解决问题![=31=]
我有 1 个 UITabbarController,其中 3 个 ViewControllers 属于其命名的 aVC-bVC-cVC(VC = ViewController).然后我有 1 个 MenuSideBar 和其他 3 个 ViewControllers 命名为 dVC-eVC-fVC。
我想要的只是每当我按下 dVC 或 eVC 或 fVC 时,我的 UITabbarController 总是显示。我该怎么做?我真的卡在了这一点上。我必须尝试很多方法,甚至自己自定义 tabbarController 但仍然无法正常工作。
请帮帮我。在这个案子上我真的需要帮助。
非常感谢。
我在 Objective C 中使用 SWRevealViewController 为您制作了示例演示,正如您在问题中标记的那样。我用的是XCode7.1.1版本。确保在 XCode 的 7.x 版本中打开此演示。这个问题也可以通过其他侧边菜单库解决,比如 MFSideMenu 等
这是我根据您的要求制作的demo样例link
https://github.com/RajanMaheshwari/TabBarControllerApp
解释:
为了在您的应用程序中始终保留标签栏,您必须在每个控制器上添加一个 TabBar,这不是 UITabBarController
的 viewController 添加。在我的示例演示中,我采用了各种控制器
1.LeftSideViewController
- 这将是整个应用程序的左侧面板。
2.MainTabBarViewController
- 这将是 UITabBarController
subclass 并且它将连接到另外两个视图控制器成为 MainTabBarViewController
.
的一部分
----a.TabFirstViewController
----b.TabSecondViewController
3.FirstViewController
- 这将是来自 LeftSideViewController 的 table 视图并具有它自己的 UITabBar
添加。
4.SecondViewController
- 这将是来自 LeftSideViewController 的 table 视图并具有它自己的 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 parent class 的控制器中,我们将添加一个 tabBar 并将 tabBar 的委托设置为 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
希望这能解决问题![=31=]