zoukankan      html  css  js  c++  java
  • iOS 一种很方便的构造TarBar

    直接在TarBarController中操作,代码如下:

    #import "DLTabBarController.h"
    #import "ViewController.h"
    #import "TwoViewController.h"
    #import "ThreeViewController.h"
    #import "FourViewController.h"
    #import "FiveViewController.h"
    #import "DLNavigationController.h"
    
    
    #define kClassKey           @"rootVCClassString" // 控制器对应的字符串
    #define kTitleKey           @"title"             // title可以当做tabBarItem的title属性
    #define kImageKey           @"imageName"         // image显示
    #define kSelImageKey        @"selectedImageName" // 被选中后的image显示
    #define kNavigationTitleKey @"navigationItemTitle" // navigationItem的title属性
    
    
    @interface DLTabBarController ()
    
    @end
    
    @implementation DLTabBarController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 构造数组(为了防止控制器写错,应该现在外面写好后再复制粘贴进去)
        NSArray *childItemsArray = @[
                                     @{
                                         kClassKey           : @"ViewController",
                                         kTitleKey           : @"首页",
                                         kImageKey           : @"home",
                                         kSelImageKey        : @"home_select",
                                         kNavigationTitleKey : @"首页"
                                         },
                                     @{
                                         kClassKey           : @"TwoViewController",
                                         kTitleKey           : @"分类",
                                         kImageKey           : @"category",
                                         kSelImageKey        : @"category_select",
                                         kNavigationTitleKey : @"分类"
                                         },
                                     @{
                                         kClassKey           : @"ThreeViewController",
                                         kTitleKey           : @"",
                                         kImageKey           : @"center",
                                         kSelImageKey        : @"center_select",
                                         kNavigationTitleKey : @"home"
                                         },
                                     @{
                                         kClassKey           : @"FourViewController",
                                         kTitleKey           : @"购物车",
                                         kImageKey           : @"cart",
                                         kSelImageKey        : @"cart_select",
                                         kNavigationTitleKey : @"购物车"
                                         },
                                     @{
                                         kClassKey           : @"FiveViewController",
                                         kTitleKey           : @"",
                                         kImageKey           : @"mine",
                                         kSelImageKey        : @"mine_select",
                                         kNavigationTitleKey : @""
                                         }
                                     ];
        
        // 遍历字典得到对应的属性
        [childItemsArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIViewController *vc = [[NSClassFromString(dict[kClassKey]) alloc] init];
            vc.navigationItem.title = dict[kNavigationTitleKey];
            vc.tabBarItem.title = dict[kTitleKey];
            
            DLNavigationController *nav = [[DLNavigationController alloc] initWithRootViewController:vc];
            
            vc.tabBarItem.image = [[UIImage imageNamed:dict[kImageKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            vc.tabBarItem.selectedImage = [[UIImage imageNamed:dict[kSelImageKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            [self addChildViewController:nav];
        }];
        
    }

    本人是很喜欢这种做法的,虽然在写 key 对应的值的时候可能出错。但是对于后面的操作我们可以省去不少的代码。所以我在项目中基本都用户这个操作

  • 相关阅读:
    Linux学习之路:shell变量(二)环境变量
    LVS配置与安装
    Linux学习之路:shell变量(一)
    SQL Server用存储过程新建视图
    【转】SQL Server 2012 配置AlwaysOn(三)
    【转】SQL 语句:Alter table
    Linux学习之路:认识shell和bash
    【转】Centos配置yum源
    【Azure 应用服务】App Service 无法连接到Azure MySQL服务,报错:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
    【Azure API 管理】在APIM 中添加 logtoeventhub 策略,把 Request Body 信息全部记录在Event Hub中
  • 原文地址:https://www.cnblogs.com/peaker-wu/p/5531272.html
Copyright © 2011-2022 走看看