[英]How to Navigate from one view to another view in ios?
I have tab bar with 4 options. 我有四个选项卡。
In the 1st tab, I have a button. 在第一个标签中,我有一个按钮。 When i click this button, I have to move from one view to another view in second tab bar.
当我单击此按钮时,我必须在第二个选项卡栏中从一个视图移动到另一个视图。
the code I am currently using is ( where shipVC
is the second tab's viewController
): 我当前使用的代码是( 其中
shipVC
是第二个选项卡的viewController
):
[shipVC.navigationController pushViewController:cartVC animated:NO];
So basically, When the button in the 1st tab viewController
is pressed, I want to move from 'X' view to 'Y' view in the 2nd tab's viewController
所以基本上,当按下第一个选项卡
viewController
的按钮时,我想从第二个选项卡的viewController
中的“ X”视图移至“ Y”视图
One quick way to do this is by using NSNotificationCenter
to post a notification when the button in the first tab's viewController
is pressed. 一种快速的方法是使用
NSNotificationCenter
在按下第一个选项卡的viewController
的按钮时发布通知。
viewController
's -viewDidLoad
method: viewController
的-viewDidLoad
方法中:
viewController
's button method: viewController
的button方法中:
Your 2nd tab's viewController
class: 您第二个选项卡的
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
}
Button method in the 1st tab's viewController
class: 第一个选项卡的
viewController
类中的Button方法:
-(IBAction)someBtnClick:(UIButton *)sender {
//...
//you can use this to post a notification from anywhere now
[[NSNotificationCenter defaultCenter] postNotificationName:@"AnyNameYouWantForNotification"
object:nil];
//...
}
someBtnClick
) will post a notification with the name AnyNameYouWantForNotification
someBtnClick
)将发布名称为AnyNameYouWantForNotification
的通知。 AnyNameYouWantForNotification
as it's name. AnyNameYouWantForNotification
的通知。
doTheNavigation
) doTheNavigation
) check my answer 检查我的答案
-(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;
}
You need to also make application with Naigationcontroller in your appdelegate 您还需要在您的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];
after that your navigationbar will appear in your first viewcontroller then you can navigate. 之后,导航栏将出现在第一个ViewController中,然后就可以导航了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.