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 对应的值的时候可能出错。但是对于后面的操作我们可以省去不少的代码。所以我在项目中基本都用户这个操作

  • 相关阅读:
    sql语句的删除
    JDK 11中的ZGC-一种可扩展的低延迟垃圾收集器
    强大的IDEA开发工具
    微信H5支付证书过滤
    statusText 报错parsererror
    python 爬取页面数据,生成词云和网络图
    springboot 跳转页面
    org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'xxx' is not present 报错解决
    airtest 中报错 'gbk' codec can't encode character 'xa5' in position 170: illegal multibyte sequence
    Error:(5, 45) java: 程序包org.springframework.boot.test.context不存在 解决
  • 原文地址:https://www.cnblogs.com/peaker-wu/p/5531272.html
Copyright © 2011-2022 走看看