[英]Trigger view controller in a navigation controller from tab bar controller
I have an app that has a tab bar controller that houses 5 navigation controllers. 我有一个带有标签栏控制器的应用程序,其中包含5个导航控制器。 Each nav controller has a menu as the first view controller however when the tab is selected I want to bypass the menu and go to the next view controller.
每个导航控制器都有一个菜单作为第一个视图控制器,但是当选择选项卡时,我要绕过菜单并转到下一个视图控制器。
Flow: 流:
TabBar Controller -> Nav Controller -> Menu -> View Controller 1 -> View Controller (level 2) TabBar控制器->导航控制器->菜单-> View Controller 1-> View Controller(level 2)
-> Nav Controller -> Menu -> View Controller 2 -> View Controller (level 2)
-> Nav Controller -> Menu -> View Controller 3 -> View Controller (level 2)
-> Nav Controller -> Menu -> View Controller 4 -> View Controller (level 2)
You could use the NSNotificationCenter to post notifications to your View Controllers when the Tab Bar gets tapped. 当标签栏被点击时,您可以使用NSNotificationCenter将通知发布到视图控制器。 To set up this mechanism you have to do this in your first view Controller.
要设置此机制,您必须在第一个View Controller中执行此操作。
#import "FirstViewController.h"
@interface FirstViewController ()<UITabBarControllerDelegate>
@end
@implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (((UINavigationController *)viewController).topViewController == self) {
//Push the next view controller to skip the menu
//Replace newViewController with the disired one
UIViewController *newViewController = [UIViewController new];
[self.navigationController pushViewController:newViewController animated:NO];
NSLog(@"Push view controller first tab");
}else {
//Post a notification to inform the other view Controller
[[NSNotificationCenter defaultCenter]postNotificationName:@"tabBarControllerDidSelectViewController" object:((UINavigationController *)viewController)];
}
}
@end
In your the rest of your view controllers do the following 在其余的视图控制器中,请执行以下操作
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didTabed:) name:@"tabBarControllerDidSelectViewController" object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
-(void)didTabed:(NSNotification *)notification
{
if ([[notification object] isKindOfClass:[UINavigationController class]]) {
UIViewController *selectedViewController = ((UINavigationController *)[notification object]).topViewController;
if (selectedViewController == self) {
//Push the next view controller to skip the menu
//Replace newViewController with the disired one
UIViewController *newViewController = [UIViewController new];
[self.navigationController pushViewController:newViewController animated:NO];
NSLog(@"Push view Controller tap2");
}
}
}
@end
If you have any questions about this please don't hesitate to ask! 如果对此有任何疑问,请不要犹豫!
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.