zoukankan      html  css  js  c++  java
  • //快速添加一个视图控制器

    //快速添加一个视图控制器


    #import <UIKit/UIKit.h>

    @interface UITabBarController (ZJTabBarExtension)

    //快速添加一个视图控制器

    -(UIViewController*)andViewController:(Class)cls title:(NSString*)title image:(NSString*)image;

    @end

    @implementation UITabBarController (ZJTabBarExtension)

    -(UIViewController*)andViewController:(Class)cls title:(NSString*)title image:(NSString*)image

    {

    //cls 传入界面所对应的类型

    //    创建视图控制器

        UIViewController*vc=[[cls alloc]init];

        vc.title=title;

        UINavigationController*nc=[[UINavigationController alloc]initWithRootViewController:vc];

        nc.tabBarItem.image=[UIImage imageNamed:image];

        

    //添加到标签栏中

        NSMutableArray *marr =[[NSMutableArray alloc]initWithArray:self.viewControllers];

        [marr addObject:nc];

        self.viewControllers=marr;

    //    返回创建的视图控制器

        

        return vc;

     }

    接着在需要添加标签控制器和导航控制器的视图中添加到delegate引入头文件

    #import "AppDelegate.h"

    #import "LimitViewController.h"

    #import "FreeViewController.h"

    #import "SaleViewController.h"

    #import "TopicViewController.h"

    #import "HotViewController.h"

    #import "UITabBarController+ZJTabBarExtension.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // Override point for customization after application launch.

    //    创建导航栏和标签栏

        [self creatTabBar];

        return YES;

    }

    -(void)creatTabBar

    {

    //  AppList()

    //    Limit(AppList)

    //    Sale(AppList)

    //     Free(AppList)

    //    Topic(Root)

    //    Hot(AppList)

    //以前的繁琐方法

    //    LimitViewController*lvc=[[LimitViewController alloc]init];

    ////    lvc.navigationItem.title=

    ////    lvc.navigationItem.image=

    //    UINavigationController*nc =[[UINavigationController alloc]initWithRootViewController:lvc];

        

        -现在的方法

        UITabBarController *tbc=[[UITabBarController alloc]init];

        [tbc andViewController:[LimitViewController class] title:@"限免" image:@"tabbar_limitfree.png"];

            [tbc andViewController:[SaleViewController class] title:@"降价" image:@"tabbar_reduceprice.png"];

            [tbc andViewController:[FreeViewController class] title:@"免费" image:@"tabbar_appfree.png"];

            [tbc andViewController:[TopicViewController class] title:@"专题" image:@"tabbar_subject.png"];

            [tbc andViewController:[HotViewController class] title:@"热榜" image:@"tabbar_rank.png"];

            self.window.rootViewController=tbc;

        self.window.backgroundColor=[UIColor whiteColor];

        代码量大大减少

    }

  • 相关阅读:
    单片机与嵌入式系统中C语言的位运算小记
    #ifndef、#def、#endif说明
    Freertos学习初识任务函数
    IAR(EWARM)下移植FreeRTOS到STM32F10x笔记
    visio 画 弯曲 箭头 ( 波浪线 曲线)
    dos 中tree的使用方法
    Win7下Borland C++ 4.5 & TASM5.0调试uC/OSII
    (*(volatile unsigned long *)
    有关推挽输出、开漏输出、复用开漏输出、复用推挽输出以及上拉输入、下拉输入、浮空输入、模拟输入区别
    POJ 1236 Network of Schools
  • 原文地址:https://www.cnblogs.com/JeinoZT/p/4403892.html
Copyright © 2011-2022 走看看