• iOS UINavigationController与UITabBarController的组合使用


     

    1.导航类型

    UINavigationController 适用于父子页面的跳转

    UITabBarController 适用于平级页面的跳转

    2.presentViewController / dismissViewControllerAnimated和pushViewController / popViewController

    (1)只有从UINavigationController导航过来的UIViewController 才可以使用pushViewController / popViewController,从其它导航进入的只能使用presentViewController / dismissViewControllerAnimated

    (2)如果A界面是通过presentViewController进入的,那么在A界面里只能使用presentViewController导航,如果A界面是通过UINavigationController包装了一层进入,则可使用pushViewController导航。

    (3)如果A界面是一个UITabBarController,进入时是通过presentViewController来导航的,那么子Tab里也只能通过presentViewController来导航,如果进入时是通过UINavigationController包装了一层进入,则可使用[self.tabBarController.navigationController pushViewController:abcdViewController animated:true];这种方式导航。

    3.修改UINavigationController导航进入后导航栏的图标及文字

    UIBarButtonItem* leftBar = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(homeBack)];

    self.navigationItem.leftBarButtonItem = leftBar;

    //可添加多个导航按钮

    //    self.navigationItem.leftBarButtonItems = nil;

    //同样右边的导航按钮也可以设置

     

    4.创建UITabBarController时需注意,要先创建出来,然后通过UINavigationController包装后才能显示,否则tab子项不显示。创建格式如下

    Java代码  收藏代码
    1. TabOneViewController* oneVC = [[TabOneViewController alloc]init];  
    2.     oneVC.view.backgroundColor = [UIColor whiteColor];  
    3.     UITabBarItem* item1 = [[UITabBarItem alloc]initWithTitle:@"tab1" image:nil tag:0];  
    4.     oneVC.tabBarItem = item1;  
    5.       
    6.     TabTwoViewController* twoVC = [[TabTwoViewController alloc]init];  
    7.     UITabBarItem* item2 = [[UITabBarItem alloc]initWithTitle:@"tab2" image:nil tag:1];  
    8.     twoVC.tabBarItem = item2;  
    9.   
    10.     HomeViewController* homeVC = [[HomeViewController alloc]init];  
    11.     NSArray* array = [[NSArray alloc]initWithObjects:oneVC,twoVC, nil];  
    12.     homeVC.viewControllers = array;  
    13.       
    14.     UINavigationController* navVC = [[UINavigationController alloc]initWithRootViewController:homeVC];  
    15.     [self presentViewController:navVC animated:YES completion:^{}];  
    16.       
    17.     //下面这种创建了HomeViewController后,HomeViewController下的子Tab不能通过push或者pop导航  
    18.     //    HomeViewController* homeVC = [[HomeViewController alloc]init];  
    19.     //    [self presentViewController:homeVC animated:YES completion:^{}];  

     

     

    5.UITabBarController各页面之间的切换

    实现UITabBarControllerDelegate协议

    //实现协议方法,用于切换Tab时,更改页面的标题
    -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
        NSInteger index = tabBarController.selectedIndex;
        NSString *title;
        switch (index) {
            case 0:
                title = @"Message";
                break;
            case 1:
                title = @"User List";
                break;
        }
        self.title 
  • 相关阅读:
    【初码干货】使用阿里云开放搜索服务快速搭建资源搜索网站
    很认真的聊一聊程序员的自我修养
    【初码干货】使用阿里云邮件推送服务架设自己邮件验证与推送体系
    Supervisor 修改配置文件中的参数值,需要更新服务才能生效
    如何关闭 IntelliJ IDEA 的 Find in Path ?
    Jenkins 服务启动/关闭/重启命令,设置端口
    java.util.ConcurrentModificationException 问题处理
    MySQL 将字符串类型的小数转换为保留位数的小数类型
    Apache Tomcat 文件包含漏洞(CVE-2020-1938)
    Error EElFTPSError: Data channel transfer error (error code is 10054) MobaXterm
  • 原文地址:https://www.cnblogs.com/idxdm/p/4569597.html
Copyright © 2020-2023  润新知