我有1个UITabbarController,其中3个ViewControllers属于其命名的aVC-bVC-cVC(VC = ViewController)。然后我有1个MenuSideBar有其他3个ViewControllers名为dVC-eVC-fVC。
我想要的只是当我推送到dVC或eVC或fVC我的UITabbarController总是显示。我怎么能这样做?我真的卡在这一点上。我必须尝试很多方法甚至自己定制tabbarController但仍然无法正常工作。
请帮帮我。我真的需要这方面的帮助。
非常感谢你。
答案 0 :(得分:1)
我已经使用 SWRevealViewController 在目标C 中为您制作了示例演示,因为您在问题中标记了该示例。我使用了 XCode 7.1.1 版本。请务必在 7.x 版本的XCode中打开此演示。这个问题也可以通过 MFSideMenu 等其他侧面菜单库来解决
以下是我根据您的要求制作的演示样本的链接 https://github.com/RajanMaheshwari/TabBarControllerApp
<强>说明强>
为了始终在应用中保留标签栏,您必须在每个控制器上添加一个TabBar,而不是UITabBarController
添加的viewController。在我的示例演示中,我采用了各种控制器
1。 LeftSideViewController
- 这将是整个应用的左侧面板。
2。 MainTabBarViewController
- 这将是UITabBarController
子类,它将连接到另外两个视图控制器,它们将成为MainTabBarViewController
的一部分。
----的一个。强> TabFirstViewController
----的湾强> TabSecondViewController
3。 FirstViewController
- 将来自 LeftSideViewController&#39; 表格视图,并添加了自己的UITabBar
点。
4。 SecondViewController
- 将来自 LeftSideViewController&#39; 表格视图,并添加了自己的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 父类的控制器中,我们将添加一个tabBar并将tabBar的委托设置为 self 并添加tabBar&#39; s 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
假设有一个表用于从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
希望这能解决问题!