如何在ios中从一个视图导航到另一个视图?

时间:2013-12-25 06:51:17

标签: ios cocoa-touch uinavigationcontroller uitabbarcontroller

我有4个选项的标签栏 在第一个标签中,我有一个按钮。当我单击此按钮时,我必须在第二个标签栏中从一个视图移动到另一个视图。

我目前使用的代码是( shipVC是第二个标签的viewController ):

[shipVC.navigationController pushViewController:cartVC animated:NO];

基本上,当按下第一个标签viewController中的按钮时,我想在第二个标签的viewController

中从“X”视图移动到“Y”视图

3 个答案:

答案 0 :(得分:2)

执行此操作的一种快捷方法是,在按下第一个标签NSNotificationCenter中的按钮时,使用viewController发布通知。

步骤:

  1. 在第二个标签的viewController -viewDidLoad方法中:
    • 添加通知观察者并为其设置目标选择器方法
  2. 在第一个标签的viewController按钮方法中:
    • 发布通知
  3. 实施例

    您的第二个标签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];

之后,您的导航栏将出现在您的第一个视图控制器中,然后您可以导航。