我有4个选项的标签栏 在第一个标签中,我有一个按钮。当我单击此按钮时,我必须在第二个标签栏中从一个视图移动到另一个视图。
我目前使用的代码是( shipVC
是第二个标签的viewController
):
[shipVC.navigationController pushViewController:cartVC animated:NO];
基本上,当按下第一个标签viewController
中的按钮时,我想在第二个标签的viewController
答案 0 :(得分:2)
执行此操作的一种快捷方法是,在按下第一个标签NSNotificationCenter
中的按钮时,使用viewController
发布通知。
viewController
-viewDidLoad
方法中:
viewController
按钮方法中:
您的第二个标签viewController
课程:
-(void)viewDidLoad {
[super viewDidLoad];
[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doTheNavigation)
name:@"AnyNameYouWantForNotification"
object:nil];
}
-(void)doTheNavigation {
[self.navigationController pushViewController:cartVC animated:NO];
//WARNING: don't just push, put a check or else repeated click on the
//button in the 1st tab will push cartVC again and again.
//This will not only warrant a crash but look very ugly
}
第一个标签viewController
课程中的按钮方法:
-(IBAction)someBtnClick:(UIButton *)sender {
//...
//you can use this to post a notification from anywhere now
[[NSNotificationCenter defaultCenter] postNotificationName:@"AnyNameYouWantForNotification"
object:nil];
//...
}
someBtnClick
的)将发布名为AnyNameYouWantForNotification
的通知AnyNameYouWantForNotification
的通知。
doTheNavigation
)答案 1 :(得分:0)
检查我的答案
-(void)settabbar{
UIViewController *viewController1 = [[[viewController1 alloc] initWithNibName:@"viewController1" bundle:nil] autorelease];
UIViewController *viewController2 = [[[viewController2 alloc] initWithNibName:@"viewController2" bundle:nil] autorelease];
UIViewController *viewController3 = [[[viewController3 alloc] initWithNibName:@"viewController3" bundle:nil] autorelease];
navControl1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navControl2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navControl3=[[UINavigationController alloc]initWithRootViewController:viewController3];
navControl1.navigationBar.tintColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
navControl2.navigationBar.tintColor=[UIColor blackColor];
navControl3.navigationBar.tintColor=[UIColor blackColor];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate=self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navControl1,navControl2,navControl3, nil ];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:@""] withFinishedUnselectedImage:[UIImage imageNamed:@""]];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setTitle:@"view1"];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[UIImage imageNamed:@"tab2.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setTitle:@"view2"];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setImage:[UIImage imageNamed:@"tab11.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setTitle:@"ciew3"];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setImage:[UIImage imageNamed:@"tab5.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:3] setTitle:@"Maintain Customer"];
[[[[self.tabBarController tabBar] items] objectAtIndex:3] setImage:[UIImage imageNamed:@"tab4.png"]];
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"trans.png"]];
UIImage* tabBarBackground = [UIImage imageNamed:@""];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"trans.png"]];
}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController*)viewController{
NSLog(@"select : %d",tabBarController.tabBar.selectedItem.tag);
if(self.tabBarController.tabBar.selectedItem.tag==0){
[[self.tabBarController.viewControllers objectAtIndex:0] popViewControllerAnimated:YES];
}else if(self.tabBarController.tabBar.selectedItem.tag==1){
[[self.tabBarController.viewControllers objectAtIndex:1] popViewControllerAnimated:YES];
}else if(self.tabBarController.tabBar.selectedItem.tag==2){
[[self.tabBarController.viewControllers objectAtIndex:2] popViewControllerAnimated:YES];
}else {
[(UINavigationController*) viewController popToRootViewControllerAnimated:YES];
}
return YES;
}
答案 2 :(得分:-1)
您还需要在appdelegate中使用Naigationcontroller进行应用程序
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
HomeViewController *homeView = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView animated:YES];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:homeView];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
之后,您的导航栏将出现在您的第一个视图控制器中,然后您可以导航。