简体   繁体   English

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

[英]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的按钮时发布通知。

Steps: 脚步:

  1. in the 2nd tab's viewController 's -viewDidLoad method: 在第二个选项卡的viewController-viewDidLoad方法中:
    • Add a notification observer and set a target selector method to it 添加通知观察器并为其设置目标选择器方法
  2. in the 1st tab's viewController 's button method: 在第一个选项卡的viewController的button方法中:
    • Post the notification 发布通知

Example: 例:

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];
    //...
}

So... 所以...

  • When the button in the 1st tab is clicked, the button action ( that I named someBtnClick ) will post a notification with the name AnyNameYouWantForNotification 单击第一个选项卡中的按钮时,按钮操作( 我命名为someBtnClick )将发布名称为AnyNameYouWantForNotification的通知。
  • The 2nd tab ( should be already loaded and ready ) will be listening for a notification that has AnyNameYouWantForNotification as it's name. 第二个选项卡( 应该已经加载并准备就绪 )将监听名称为AnyNameYouWantForNotification的通知。
    • When this notification is received, it will execute the linked selector method ( that I named 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.

相关问题 iOS无法从表视图控制器导航到另一个 - IOS can't navigate from a table view controller to another one 如何从一个视图控制器导航到另一视图控制器? - How to navigate from one view controller to another view controller? 如何通过单击按钮在 Swift UI 中从一个视图导航到另一个视图 - How to navigate from one view to another in Swift UI on a click of button 如何在 Swift 中以编程方式从一个控制器导航到另一个视图控制器? - How to navigate from one controller to another View Controller programmatically in Swift? 如何从一个视图导航到另一个视图而无需导航? 在iPhone中 - how to navigate from one view to another without navigation? in iphone 如何在不使用 NavigationController 的情况下从一个视图导航到另一个视图? - How navigate from one view to another without using NavigationController? 如何使用 Swift 从一个视图控制器导航到另一个视图控制器 - How to Navigate from one View Controller to another using Swift 使用UIButton从一个视图导航到另一个视图 - Navigate from one view to another using UIButton 如何在IOS中将数据从一个视图传递到另一个视图控制器? - How to pass data from one view to another view controller in IOS? IOS 从另一个视图访问一个视图 - IOS Accessing one view from another view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM